hdu 1853 Cyclic Tour【KM】
来源:互联网 发布:access2007制作数据库 编辑:程序博客网 时间:2024/05/18 03:01
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853
我的链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=17728#problem/C
KM算法模板:http://blog.csdn.net/cfreezhan/article/details/8246639
用KM求最小费用模板题:http://blog.csdn.net/cfreezhan/article/details/8256926
Cyclic Tour
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total Submission(s): 857 Accepted Submission(s): 436
Problem Description
There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total length of all the tours minimum, but he is too lazy to calculate. Can you help him?
Input
There are several test cases in the input. You should process to the end of file (EOF).
The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).
The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).
Output
Output one number for each test case, indicating the minimum length of all the tours. If there are no such tours, output -1.
Sample Input
6 91 2 52 3 53 1 103 4 124 1 84 6 115 4 75 6 96 5 46 51 2 12 3 13 4 14 5 15 6 1
Sample Output
42-1HintIn the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.
Author
RoBa@TJU
Source
HDU 2007 Programming Contest - Final
Recommend
lcy
题意:找出若干个环,覆盖所有的点,使得总花费最小。
思路:以城市的个数建图,初始化为最小值(大负数),注意重复路径,以长度的负值求KM,
从而求出最大匹配,返回负值即是最短路径。
//Accepted 300 KB 46 ms C++ 1463 B 2013-02-26 15:42:24
//Accepted300 KB46 msC++1463 B2013-02-26 15:42:24#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=110;const int minn=-1<<30;char map[maxn][maxn];int w[maxn][maxn];int lx[maxn],ly[maxn];//顶标 bool s[maxn],t[maxn];//s[i]、t[i]为左/右第i个点是否已标记int match[maxn];int n;int hungary(int u)//匈牙利,匹配(找增广路) {s[u]=true;//标记入匈牙利树 for(int v=1;v<=n;v++){if(!t[v] && lx[u]+ly[v]==w[u][v]){t[v]=true;//标记入匈牙利树 易忘记 if(match[v]==-1 || hungary(match[v])){match[v]=u;return true;}}}return false;}int KM(){int sum=0;memset(match,-1,sizeof(match));//初始化,易写错 for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)lx[i]=-1<<30;//初始化 顶标 memset(ly,0,sizeof(ly));for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)lx[i]=max(lx[i],w[i][j]);//最大权 for(int i=1;i<=n;i++)//匹配每一个点 {while(true){memset(s,false,sizeof(s));//每次匹配前,找增广路,初始化为0 memset(t,false,sizeof(t));if(hungary(i)) break;else{int a=1<<30;for(int j=1;j<=n;j++) if(s[j])//j匹配过(即在匈牙利树中) for(int k=1;k<=n;k++) if(!t[k] && a>lx[j]+ly[k]-w[j][k])a=lx[j]+ly[k]-w[j][k];for(int j=1;j<=n;j++)//修改顶标 {if(s[j]) lx[j]-=a;if(t[j]) ly[j]+=a;}}}}for(int i=1;i<=n;i++) sum+=w[match[i]][i];for(int i=1;i<=n;i++) if(w[match[i]][i]==minn) return 1;//匹配不成功 return sum;}int main(){int N,M;int u,v,length;while(scanf("%d%d",&N,&M)!=EOF){n=N;for(int i=1;i<=N;i++)for(int j=1;j<=N;j++)w[i][j]=minn;for(int i=1;i<=M;i++){scanf("%d%d%d",&u,&v,&length);if(-length>w[u][v]) w[u][v]=-length;}printf("%d\n",-KM());}return 0;}
- hdu 1853 Cyclic Tour //km
- hdu 1853 Cyclic Tour【KM】
- hdu 1853 Cyclic Tour (KM)
- hdu 1853 Cyclic Tour KM
- HDU 1853 Cyclic Tour KM算法
- HDU 1853 Cyclic Tour && HDU 3488Tour KM算法
- HDU 1853 Cyclic Tour(KM完美匹配)
- hdu 1853 Cyclic Tour(KM 最小权值匹配)
- HDU-1853 Cyclic Tour
- hdu 1853 Cyclic Tour
- HDU 1853Cyclic Tour
- HDU_1853 Cyclic Tour KM
- HDU1853-Cyclic Tour KM
- HDU 1853 Cyclic Tour & HDU 3488 Tour
- hdu 1853 Cyclic Tour && hdu 3435 A new Graph Game(简单KM算法)
- hdu(1853)Cyclic Tour
- hdu 1853(Cyclic Tour)
- hdu acm 1853 Cyclic Tour
- linux进程状态D的一种产生原因
- smtp命令
- jQuery Mobile 1.3.0正式版发布了!
- pat 1020 Tree Traversals
- 【vim】复制一个文件中的数据到另一个文件中(使用缓冲区)
- hdu 1853 Cyclic Tour【KM】
- Objective-C 学习笔记
- 使用jaxb-xjc的xjc和schemagen
- php随机生成国内ip地址
- 3DS文件在OpenGL的读入和显示
- 小桥加加的英语学习博客
- [笔记]20070820英语学习笔记
- [笔记]20070821英语学习笔记
- Cocos2dx 使用 hiero生成的字体