HDU 3488 Tour(二分图最优匹配:有向环覆盖)
2016-02-16 22:33
387 查看
题意:给你一个N个顶点M条边的带权有向图,要你把该图分成1个或多个不相交的有向环.且所有定点都被有向环覆盖.问你该有向环所有权值的总和最小是多少?(保证有解)
思路:参照HDU1853
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
Sample Output
思路:参照HDU1853
#include<cstdio> #include<cstring> #include<algorithm> #define INF 1e9 using namespace std; const int maxn=200+5; struct Max_Match { int n, W[maxn][maxn]; int Lx[maxn],Ly[maxn]; bool S[maxn],T[maxn]; int left[maxn]; bool match(int i) { S[i]=true; for(int j=1;j<=n;j++)if(Lx[i]+Ly[j]==W[i][j] && !T[j]) { T[j]=true; if(left[j]==-1 || match(left[j])) { left[j]=i; return true; } } return false; } void update() { int a=1<<30; for(int i=1;i<=n;i++)if(S[i]) for(int j=1;j<=n;j++)if(!T[j]) a=min(a, Lx[i]+Ly[j]-W[i][j]); for(int i=1;i<=n;i++) { if(S[i]) Lx[i]-=a; if(T[i]) Ly[i]+=a; } } int solve(int n) { this->n=n; memset(left,-1,sizeof(left)); for(int i=1;i<=n;i++) { Lx[i]=Ly[i]=0; for(int j=1;j<=n;j++) Lx[i]=max(Lx[i], W[i][j]); } for(int i=1;i<=n;i++) { while(true) { for(int j=1;j<=n;j++) S[j]=T[j]=false; if(match(i)) break; else update(); } } int ans=0; for(int i=1;i<=n;i++) ans += W[left[i]][i]; return -ans; } }KM; int main() { int T; scanf("%d",&T); while(T--) { int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) KM.W[i][j]=-INF; while(m--) { int u,v,w; scanf("%d%d%d",&u,&v,&w); KM.W[u][v]= max(KM.W[u][v], -w); } printf("%d\n",KM.solve(n)); } return 0; }
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
相关文章推荐
- keepalived基础配置详解(一)
- canvas+video实现跨域视频截图
- C++虚析构函数
- 2016年读书计划
- Spring REST是什么?(转)
- RxJava开发精要6 – Observables组合
- 手淘适配设置模板
- Servlet执行过程
- block 注意事项 和陷阱
- 我想创业
- RxJava开发精要5 – Observables变换
- slf4j与logback的结合使用
- TI CC254X函数库说明续(BLE GATT.h)
- spring framework 下载地址
- 215 Kth Largest Element in an Array
- HDU 1853 Cyclic Tour(二分图最优匹配:有向环覆盖)
- java 反编译利器JD-JUI
- 关于 从别人电脑上 高版本的 Xcode上拷贝过来的项目的 不能运行模拟器的 解决方法
- 如何创建一个web项目
- MySQL乱码问题解决总结