【HDU3488】【匹配】【KM最小费用圈】
2015-09-05 10:04
197 查看
Tour
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2324 Accepted Submission(s): 1166
Problem Description
In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one-way roads connecting them. You are lucky enough to have a chance to have a tour in the kingdom. The route should be designed as: The route should contain one or more loops.
(A loop is a route like: A->B->……->P->A.)
Every city should be just in one route.
A loop should have at least two cities. In one route, each city should be visited just once. (The only exception is that the first and the last city should be the same and this city is visited twice.)
The total distance the N roads you have chosen should be minimized.
Input
An integer T in the first line indicates the number of the test cases.
In each test case, the first line contains two integers N and M, indicating the number of the cities and the one-way roads. Then M lines followed, each line has three integers U, V and W (0 < W <= 10000), indicating that there is a road from U to V, with the
distance of W.
It is guaranteed that at least one valid arrangement of the tour is existed.
A blank line is followed after each test case.
Output
For each test case, output a line with exactly one integer, which is the minimum total distance.
Sample Input
1 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
Sample Output
42
Source
2010 ACM-ICPC Multi-University
Training Contest(6)——Host by BIT
Recommend
zhouzeyong | We have carefully selected several similar problems for you: 1533 3435 1853 3395 3491
#include <iostream> #include <cstring> #include <cmath> #include <queue> #include <stack> #include <list> #include <map> #include <set> #include <string> #include <cstdlib> #include <cstdio> #include <algorithm> using namespace std; const int N = 220; const int INF = 0x3f3f3f3f; int nx,ny;//两边的点数 int g ;//二分图描述 int linker ,lx ,ly ;//y中各点匹配状态,x,y中的点标号 int slack ; bool visx ,visy ; bool DFS(int x) { visx[x] = true; for(int y = 0; y < ny; y++) { if(visy[y])continue; int tmp = lx[x] + ly[y] - g[x][y]; if(tmp == 0) { visy[y] = true; if(linker[y] == -1 || DFS(linker[y])) { linker[y] = x; return true; } } else if(slack[y] > tmp) slack[y] = tmp; } return false; } int KM() { memset(linker,-1,sizeof(linker)); memset(ly,0,sizeof(ly)); for(int i = 0;i < nx;i++) { lx[i] = -INF; for(int j = 0;j < ny;j++) if(g[i][j] > lx[i]) lx[i] = g[i][j]; } for(int x = 0;x < nx;x++) { for(int i = 0;i < ny;i++) slack[i] = INF; while(true) { memset(visx,false,sizeof(visx)); memset(visy,false,sizeof(visy)); if(DFS(x))break; int d = INF; for(int i = 0;i < ny;i++) if(!visy[i] && d > slack[i]) d = slack[i]; for(int i = 0;i < nx;i++) if(visx[i]) lx[i] -= d; for(int i = 0;i < ny;i++) { if(visy[i])ly[i] += d; else slack[i] -= d; } } } int res = 0; for(int i = 0;i < ny;i++) if(linker[i] != -1) res += g[linker[i]][i]; return res; } int T; int main() { scanf("%d",&T); while(T --) { int n,m; memset(g,~INF,sizeof(g)); scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); u --; v --; g[u][v] = max(g[u][v],-w); } nx = n; ny = n; printf("%d\n",-KM()); } return 0; }
相关文章推荐
- mysql,sql server,oracle 唯一索引字段是否允许出现多个 null 值?
- 腾讯笔试题——腾讯研发工程师B笔试卷
- 过滤器模板
- HDU 1102 Constructing Roads -- prime
- hdu4414 Finding crosses
- 临界区问题的产生
- scikit-learn:matplotlib.pyplot常用画图功能总结(2)——多子图绘制
- 解决Oracle EBS出报表不可复制问题
- X-UA-Compatible属性的解释
- UML之活动图(Activity Diagram)
- POJ2513 Colored Sticks(字典树+并查集+欧拉回路)
- hdu1513 (滚动数据压缩空间)
- 获取XMLHttpRequest对象
- NOTIFYICONDATA
- I/O Stream<找出文件中最大与最小的整数>
- 百度分享_自定义分享代码
- 了解编译器默默生成哪些函数(Effective C++_5)
- Win10装机量已经7500万 免费的力量太强大了
- 欢迎使用CSDN-markdown编辑器
- 大端模式和小端模式