| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 689 人关注过本帖
标题:求高人中的高人,用匈牙利算法示范下面程序
只看楼主 加入收藏
huicpc0876
Rank: 2
等 级:论坛游民
帖 子:69
专家分:50
注 册:2009-7-24
结帖率:92.59%
收藏
已结贴  问题点数:20 回复次数:1 
求高人中的高人,用匈牙利算法示范下面程序
求教高人中的高人,用匈牙利算法写出下面程序给小弟参考学习。

今天做个题目,学长说用匈牙利算法(求最大匹配),但是程序模板不知道,求高人,帮写下面程序,
题目:电视台做了关于猫狗喜好投票,喜欢猫的观众不喜欢狗,问,电视台最多能满足都少观众需求。
 
Sample Input
2/*表示几组测试数据*/
1 1 2/*第一个数表示猫的数量,第二个数表示狗的数量,第三个数表示投票观众的数量*
C1 D1/*这是第一个人投票结果,第一票投喜欢票,第二票投反对票,所以C1表示他喜欢第一只猫,D1表示讨厌第一只狗,以下类推*/
D1 C1
1 2 4
C1 D1
C1 D1
C1 D2
D2 C1

Sample Output
1/*表示最多能满足多少观众(投票者),即投票人数减去匈牙利算法最大匹配数/
3
搜索更多相关主题的帖子: 匈牙利 高人 
2009-08-11 11:57
xujiaming007
Rank: 2
等 级:论坛游民
帖 子:4
专家分:20
注 册:2008-11-23
收藏
得分:20 
貌似这个是ACM的题目。。。。
Cat vs Dog


--------------------------------------------------------------------------------

Time limit: 2sec. Submitted: 43
Memory limit: 64M Accepted: 14

Source: NWERC 2008

--------------------------------------------------------------------------------

The latest reality show has hit the TV: "Cat vs. Dog". In this show, a bunch of cats and dogs compete for the very prestigious BEST PET EVER title. In each episode, the cats and dogs get to show themselves off, after which the viewers vote on which pets should stay and which should be forced to leave the show.

Each viewer gets to cast a vote on two things: one pet which should be kept on the show, and one pet which should be thrown out. Also, based on the universal fact that everyone is either a cat lover (i.e. a dog hater) or a dog lover (i.e. a cat hater), it has been decided that each vote must name exactly one cat and exactly one dog.

Ingenious as they are, the producers have decided to use an advancement procedure which guarantees that as many viewers as possible will continue watching the show: the pets that get to stay will be chosen so as to maximize the number of viewers who get both their opinions satisfied. Write a program to calculate this maximum number of viewers.

Input

On the first line one positive number: the number of testcases, at most 100. After that per testcase:

One line with three integers c, d, v (1 <= c, d <= 100 and 0 <= v <= 500): the number of cats, dogs, and voters.

v lines with two pet identifiers each. The first is the pet that this voter wants to keep, the second is the pet that this voter wants to throw out. A pet identifier starts with one of the characters 'C' or 'D', indicating whether the pet is a cat or dog, respectively. The remaining part of the identifier is an integer giving the number of the pet (between 1 and c for cats, and between 1 and d for dogs). So for instance, "D42" indicates dog number 42.

Output

One line with the maximum possible number of satisfied voters for the show.

Sample Input


2
1 1 2
C1 D1
D1 C1
1 2 4
C1 D1
C1 D1
C1 D2
D2 C1

Sample Output


1
3

 
下面是我AC的代码如下:
#include<cstdio>
#include<cstring>
#include<vector>
#define MAXN 2100
using namespace std;
struct NODE{
    int x,y;   
}arr[MAXN];
bool mark[MAXN],root[MAXN];
int pre[MAXN],c,d,n;
vector<int>v[MAXN];
int T(char ch,int x){
    if(ch=='C')return x;
    else return c+x;   
}
bool DFS(int x){
    for(int i=0;i<v[x].size();i++){
        int t=v[x][i];
        if(!mark[t]){
            mark[t]=true;
            if(pre[t]==-1||DFS(pre[t])){
                pre[t]=x;return true;   
            }   
        }   
    }
    return false;
}
int Max_Match(){
    int ans=0;
    memset(pre,0xff,sizeof(pre));
    memset(root,0,sizeof(root));
    for(int i=0;i<n;i++){
        memset(mark,0,sizeof(mark));
        if(DFS(i))ans++;
    }
    return n-ans/2;
}
int main(){
    char ch,ach;
    int cases,i,j,a,b;
    scanf("%d",&cases);
    while(cases--){
        scanf("%d %d %d",&c,&d,&n);
        for(i=0;i<MAXN;i++)v[i].clear();
        for(i=0;i<n;i++){
            getchar();
            scanf("%c%d %c%d",&ch,&a,&ach,&b);
            arr[i].x=T(ch,a);
            arr[i].y=T(ach,b);   
        }
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
                if(arr[i].x==arr[j].y||arr[i].y==arr[j].x)
                    v[i].push_back(j);
        printf("%d\n",Max_Match());
    }
}
2009-08-24 21:53
快速回复:求高人中的高人,用匈牙利算法示范下面程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013595 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved