hdu 3488 Tour【最小费用最大流】
来源:互联网 发布:es海量数据分析 编辑:程序博客网 时间:2024/05/26 08:42
Tour
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2992 Accepted Submission(s): 1457
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: 3435 3491 3315 3497 3157
题目大意:一个人要旅行,旅行的过程一定是从某个点出发,走一圈之后回到起点,算是一次旅行,问是否在给你n个点,m条有向边的情况下,走完所有城市,输出最小权值花费
思路:
1、此题同hdu 1853 ;
2、最开始看到需要走一个圈的时候可能稍微有点绕,不知道从何下手比较好,其实并不难,首先我们对应想到如果从一个点出发,再回到这个点,如果对应有流需要流过的话,其实对应我们可以将点i拆成两个点i,i+n,一个用来表示起点,一个用来表示终点。
3、那么问题转化到费用流上来:
①建立源点S,将源点连入各个节点(1-n),费用为0,容量为1.表示一个点只能走一次。
②建立汇点T,将各个拆点连入汇点(n+1-2n),费用为0,容量为1。
③对应m条有向边,将u连入v+n,费用为其权值,容量为INF。
那么对应一个简单的三个点组成的环的建图情况:
很明显,跑完费用流的maxflow=3;
那么如果少了3->1这条有向边,很明显maxlfow=2;
4、那么我们得到结论:建好图之后跑费用流,因为题目中保证了一定有解,那么直接输出最小花费即可。
#include<stdio.h>#include<string.h>#include<queue>#include<iostream>using namespace std;struct node{ int from; int to; int w; int num; int f; int next;}e[5000000];int head[20000];int dis[20000];int pre[20000];int path[20000];int vis[20000];int n,m,cont,ss,tt;void add(int from,int to,int w,int f){ e[cont].w=w; e[cont].f=f; e[cont].num=cont; e[cont].to=to; e[cont].next=head[from]; head[from]=cont++;}int SPFA(){ queue<int >s; s.push(ss); for(int i=1;i<=tt;i++)dis[i]=0x3f3f3f3f; dis[ss]=0; memset(vis,0,sizeof(vis)); while(!s.empty()) { int u=s.front(); s.pop();vis[u]=0; for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].to; int w=e[i].w; int f=e[i].f; if(f&&dis[v]>dis[u]+w) { dis[v]=dis[u]+w; pre[v]=u; path[v]=e[i].num; if(vis[v]==0) { vis[v]=1; s.push(v); } } } } if(dis[tt]==0x3f3f3f3f)return 0; else return 1;}void MCMF(){ int maxflow=0; int ans=0; while(SPFA()==1) { int minn=0x3f3f3f3f; for(int i=tt;i!=ss;i=pre[i]) { minn=min(minn,e[path[i]].f); } for(int i=tt;i!=ss;i=pre[i]) { e[path[i]].f-=minn; e[path[i]^1].f+=minn; } maxflow+=minn; ans+=minn*dis[tt]; } printf("%d\n",ans);}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); cont=0; ss=n*2+1; tt=ss+1; memset(head,-1,sizeof(head)); for(int i=1;i<=n;i++) { add(ss,i,0,1); add(i,ss,0,0); add(i+n,tt,0,1); add(tt,i+n,0,0); } for(int i=0;i<m;i++) { int x,y,w; scanf("%d%d%d",&x,&y,&w); add(x,y+n,w,0x3f3f3f3f); add(y+n,x,-w,0); } MCMF(); }}
- hdu 3488 Tour【最小费用最大流】
- HDU 3488 Tour 最小费用最大流||最大匹配
- HDU 1853 Cyclic Tour && HDU 3488Tour 最小费用流
- HDU 3488--Tour【最小费用最大流 && 有向环最小权值覆盖 && 经典】
- Hdu 1853 Cyclic Tour - 最小费用最大流
- hdu 1853 Cyclic Tour(最小费用最大流)
- hdu 1853 Cyclic Tour 最小费用最大流
- hdu 1853 Cyclic Tour 最小费用最大流
- HDU 1853 Cyclic Tour(最小费用最大流)
- hdu 1853 Cyclic Tour(最小费用最大流)
- hdu 1853 Cyclic Tour【最小费用最大流】
- hdu-1853 Cyclic Tour(最小费用最大流)
- HDU 3488 Tour [裂点+KM或最小费用最大流]
- hdoj 3488 Tour 【最小费用最大流】【KM算法】
- 【POJ2135】Farm Tour 最小费用最大流
- Farm Tour-最小费用最大流/SPFA
- poj2135 Farm Tour 最小费用最大流
- POJ2135 Farm Tour 【最小费用最大流】
- codeforces 707 c Pythagorean Triples 数论+勾股数
- 离开HK后的第一篇所感--重生
- Android系统篇之----解读AMS远端服务调用机制以及Activity的启动流程
- JS家的排序算法
- ubuntu14.04+opencv 3.0安装及测试
- hdu 3488 Tour【最小费用最大流】
- Zend API: config.m4配置文件的一些宏
- 接口
- 大型网站架构技能图谱(Java版)
- 单调栈的应用
- 二叉搜索树
- 异常
- Pycharm技巧(一)
- Switch能否用String做参数?