HDU1853-Cyclic Tour KM
2016-01-01 03:06
267 查看
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即可。
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 0x3f3f3f3f int 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; }
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- 初学图论-Kahn拓扑排序算法(Kahn's Topological Sort Algorithm)
- 初学图论-Bellman-Ford单源最短路径算法
- 初学图论-DAG单源最短路径算法
- 初学图论-Dijkstra单源最短路径算法
- 初学图论-Dijkstra单源最短路径算法基于优先级队列(Priority Queue)的实现
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207
- 初学ACM - 组合数学基础题目PKU 1833