首页 > 代码库 > hdu 1853 Cyclic Tour && hdu 3435 A new Graph Game(简单KM算法)
hdu 1853 Cyclic Tour && hdu 3435 A new Graph Game(简单KM算法)
Cyclic Tour
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total Submission(s): 1478 Accepted Submission(s): 750
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 9 1 2 5 2 3 5 3 1 10 3 4 12 4 1 8 4 6 11 5 4 7 5 6 9 6 5 4 6 5 1 2 1 2 3 1 3 4 1 4 5 1 5 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.
这是1853代码,后一题建成双向边就行了,代码类似。
#include"stdio.h" #include"string.h" #define N 105 #define mmax(a,b) ((a)>(b)?(a):(b)) #define mmin(a,b) ((a)<(b)?(a):(b)) const int inf=(int)1e8; int g[N][N],slack[N],n; int link[N],lx[N],ly[N]; int visx[N],visy[N]; int find(int k) { int i; visx[k]=1; for(i=1;i<=n;i++) { if(visy[i]) continue; int d=lx[k]+ly[i]-g[k][i]; if(d==0) { visy[i]=1; if(link[i]==-1||find(link[i])) { link[i]=k; return 1; } } else slack[i]=mmin(slack[i],d); } return 0; } int KM() { int i,j; memset(ly,0,sizeof(ly)); memset(link,-1,sizeof(link)); for(i=1;i<=n;i++) { lx[i]=-inf; for(j=1;j<=n;j++) { lx[i]=mmax(lx[i],g[i][j]); } } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { slack[j]=inf; } while(1) { memset(visx,0,sizeof(visx)); memset(visy,0,sizeof(visy)); if(find(i)) break; else { int d=inf; for(j=1;j<=n;j++) { if(!visy[j]) d=mmin(slack[j],d); } for(j=1;j<=n;j++) { if(visx[j]) lx[j]-=d; if(visy[j]) ly[j]+=d; } } } } int ans=0; for(i=1;i<=n;i++) { if(link[i]==-1||g[link[i]][i]==-inf) break; ans+=g[link[i]][i]; } if(i<=n) return -1; return -ans; } int main() { int i,j,u,v,w,m; while(scanf("%d%d",&n,&m)!=-1) { for(i=1;i<=n;i++) //注意初始化边为最小值 { for(j=1;j<=n;j++) { g[i][j]=-inf; } } while(m--) { scanf("%d%d%d",&u,&v,&w); g[u][v]=mmax(g[u][v],-w); } printf("%d\n",KM()); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。