Tour(hdu 3488,KM+拆点)
2013-08-19 09:07
204 查看
http://acm.hdu.edu.cn/showproblem.php?pid=3488
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=29327#problem/F
F - Tour
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
解析:
题意:
给出n个点,m条路线,要求走这样的路线,所走路线必须环,并且每个点必只属于一个环。求最短路径长度
思路:KM+拆点
这里两个问题要处理
1.这里重点在于处理走的是环且每个点只属于一个环:
拆点(每个点可以拆为初始点和终端点),要求每个点的出度==入度=1(因为只属于一个环),这里正好转化为二分匹配问题,即初始点和终端点的匹配
2.将权值取反利用KM法求最大全匹配
884 KB 296 ms C++ 1396 B
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=29327#problem/F
F - Tour
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
解析:
题意:
给出n个点,m条路线,要求走这样的路线,所走路线必须环,并且每个点必只属于一个环。求最短路径长度
思路:KM+拆点
这里两个问题要处理
1.这里重点在于处理走的是环且每个点只属于一个环:
拆点(每个点可以拆为初始点和终端点),要求每个点的出度==入度=1(因为只属于一个环),这里正好转化为二分匹配问题,即初始点和终端点的匹配
2.将权值取反利用KM法求最大全匹配
884 KB 296 ms C++ 1396 B
#include<string.h> #include<stdio.h> #include<algorithm> #include <iostream> using namespace std; const int maxn=400+10; const int inf=1<<29; int result[maxn]; int map[maxn][maxn],lx[maxn],ly[maxn]; int visx[maxn],visy[maxn]; int n,lack; int find(int x) { visx[x]=1; for(int y=1;y<=n;y++) { if(visy[y])continue; int temp=lx[x]+ly[y]-map[x][y]; if(temp==0) { visy[y]=1; if(result[y]==0||find(result[y])) { result[y]=x; return 1; } } else if(lack>temp)lack=temp; } return 0; } void KM() { memset(result,0,sizeof(result)); memset(ly,0,sizeof(ly)); memset(lx,-inf,sizeof(lx)); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(map[i][j]>lx[i]) lx[i]=map[i][j]; for(int x=1;x<=n;x++) { while(1) { memset(visx,0,sizeof(visx)); memset(visy,0,sizeof(visy)); lack=inf; if(find(x))break; for(int i=1;i<=n;i++) { if(visx[i])lx[i]-=lack; if(visy[i])ly[i]+=lack; } } } } int main() { int i,j,N,m,T; scanf("%d",&T); while(T--) { scanf("%d%d",&N,&m); n=2*N; for(i=1;i<=n;i++) for(j=1;j<=n;j++) map[i][j]=-inf; int u,v,w; for(i=1;i<=m;i++) { scanf("%d%d%d",&u,&v,&w); w=-w;//为求最小权而取反 if(map[u][v+N]<w)//防止重边 map[u][v+N]=w; } KM(); int ans=0; for(i=1;i<=n;i++) { if(map[result[i]][i]!=-inf&&result[i]!=0) ans+=map[result[i]][i]; } ans=-ans; printf("%d\n",ans); } return 0; }
相关文章推荐
- HDU-3488 Tour 再次感受到KM的强大
- HDU 3488Tour(网络流之最小费用流)
- Hdu 3488 Tour (KM 有向环覆盖)
- hdu 3488 Tour(最小费用流orKM算法)
- hdu 3488 Tour【KM】同hdu 1853
- HDU 1853 Cyclic Tour && HDU 3488 Tour 最小费用流
- HDU 3488 Tour
- hdu 3488 Tour(费用流,去重边)
- hdu - 3488 - Tour(二分图最佳完美匹配)
- HDU 3488 Tour(KM完美匹配)
- hdu 3488 Tour【KM】同hdu 1853
- hdu 3488 Tour(二分匹配 最小权完备匹配 km算法)
- HDU 3488 Tour
- HDU 3488 Tour 最大完美匹配
- HDU 3488 Tour【多个环的并】
- HDU 3488 Tour(拆点+最优匹配)
- HDU 3488 Tour(最小费用流:有向环最小权值覆盖)
- HDU 3488 Tour
- HDU-3488 Tour
- hdu - 3488 - Tour