hdu 1853 Cyclic Tour && hdu 3435 A new Graph Game(简单KM算法)
来源:互联网 发布:科比00年总决赛数据 编辑:程序博客网 时间:2024/05/17 09:22
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 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.
这是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;}
0 0
- hdu 1853 Cyclic Tour && hdu 3435 A new Graph Game(简单KM算法)
- HDU 3435 A new Graph Game //KM
- hdu 3435 A new Graph Game (KM)
- hdu 3435 A new Graph Game KM
- HDU 3435 A new Graph Game KM算法
- HDU 3435 A new Graph Game(KM完美匹配)
- HDU 3435 A new Graph Game (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 (KM)
- hdu 1853 Cyclic Tour KM
- [HDU 3435]A new Graph Game[拆点][KM]
- HDU 1853 Cyclic Tour(KM完美匹配)
- hdu 1853 Cyclic Tour(KM 最小权值匹配)
- hdu 3435 A new Graph Game
- HDU-3435 A new Graph Game
- 让他红人馆豆腐干地方大法官
- hdu oj2050杭电acm2050 折线分割平面
- poj 2236 Wireless Network(并查集)
- UVA 11401 Triangle Counting
- hdu3336Count the string
- hdu 1853 Cyclic Tour && hdu 3435 A new Graph Game(简单KM算法)
- cocos2d-x-3.0移植android
- QThread的使用总结(2)
- ubuntu 安装gcc pcre
- Single Round Match
- HDU 4883 TIANKENG’s restaurant
- 使用rgba属性兼容IE8
- 九度OJ 1021统计字符----20140728
- 炒冷饭系列:设计模式 单例模式