HDU1853-Cyclic Tour KM
来源:互联网 发布:northwind数据库下载 编辑:程序博客网 时间:2024/04/30 08:42
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).
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
-1
题意:给你N个城市M条路,求若干个环,保证每个城市在且仅在某一个环里,输出最小代价。
思路:想到每个环的每个节点都保证了出度=入度=1,就可以转化为二分图匹配了~。x点集和y点集都是给出的点集,求一遍KM即可。
#include<bits/stdc++.h>using namespace std;#define M 310#define inf 0x3f3f3f3fint nx,ny,n,m; int link[M]; int visx[M],visy[M];int w[M][M],lx[M],ly[M],slack[M];int ans[M]; int DFS(int x) { visx[x] = 1; for (int y = 1;y <= ny;y ++) { if (visy[y]) continue; int t = lx[x] + ly[y] - w[x][y]; if (t < 1e-5) { visy[y] = 1; if (link[y] == -1||DFS(link[y])) { link[y] = x; return 1; } } else if (slack[y] > t) slack[y] = t; } return 0; } void KM() { int i,j; memset (link,-1,sizeof(link)); memset (ly,0,sizeof(ly)); memset(lx,0,sizeof(lx)); for (i = 1;i <= nx;i ++) for (j = 1,lx[i] = -inf;j <= ny;j ++) if (w[i][j] > lx[i]) lx[i] = w[i][j]; for (int x = 1;x <= nx;x ++) { for (i = 1;i <= ny;i ++) slack[i] = inf; while (1) { memset (visx,0,sizeof(visx)); memset (visy,0,sizeof(visy)); if (DFS(x)) break; int a=inf; for(int i=1;i<=nx;i++)if(visx[i]) for(int j=1;j<=ny;j++)if(!visy[j]) a=min(a,lx[i]+ly[j]-w[i][j]); for(int i=1;i<=nx;i++){ if(visx[i])lx[i]-=a; if(visy[i])ly[i]+=a; } } } int res = 0,cnt = 0; for(int i = 1;i <= ny;i++) { if(link[i] > -1 && w[link[i]][i] != -inf) res+=w[link[i]][i],cnt++; } if(cnt != ny) printf("-1\n"); else printf("%d\n",-res); }int main(){ freopen("h1853.in","r",stdin); while(~scanf("%d%d",&n,&m)) { for(int i = 0;i <= n;i++) { for(int j = 0;j <= n;j++) { w[i][j] = -inf; } } for(int i = 0;i < m;i++) { int u,v,w1; scanf("%d%d%d",&u,&v,&w1); if(-w[u][v] > w1) w[u][v] = -w1; } nx = ny = n; KM(); } return 0;}
- HDU1853-Cyclic Tour KM
- HDU1853 Cyclic Tour KM算法 模版题
- hdu1853 Cyclic Tour (KM算法求最小费用流)
- HDU1853 Cyclic Tour(KM或费用流)
- #HDU1853#Cyclic Tour(Km经典模型+环)
- HDU1853 Cyclic Tour
- hdu1853 Cyclic Tour
- 【网络流】hdu1853 Cyclic Tour
- 费用流 hdu1853 Cyclic Tour
- [费用流] HDU1853 Cyclic Tour
- 【二分图完美匹配】【KM算法】hdu2255 奔小康赚大钱 && hdu1853 Cyclic Tour
- HDU_1853 Cyclic Tour KM
- 【拆点费用流】【HDU1853】【 Cyclic Tour】
- hdu 1853 Cyclic Tour //km
- hdu 1853 Cyclic Tour【KM】
- hdu 1853 Cyclic Tour (KM)
- hdu 1853 Cyclic Tour KM
- hdu1853 Cyclic Tour . 最小费用流 StL解法
- Android OkHttp完全解析 是时候来了解OkHttp了
- JMetal使用小记
- 宫崎骏和他的世界
- HDU1533-Going Home 简单KM
- okhttp去访问自签名的网站
- HDU1853-Cyclic Tour KM
- 黑马程序员——正则表达式及模式与匹配器对象
- HDU3488-Tour KM
- Gradlle 全解析(Android官方技术文档翻译)
- Android Studio开发项目中问题总结
- HDU3435-A new Graph Game KM
- Android子线程中更新UI的三种方法和获取message的两种方法总结
- 医道官途
- HDU2426-Interesting Housing Problem KM 稍微复杂一点