HDU 1853 Cyclic Tour(最小费用最大流)
来源:互联网 发布:网站域名怎么续费go 编辑:程序博客网 时间:2024/04/29 17:36
Cyclic Tour
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total Submission(s): 1879 Accepted Submission(s): 938
Problem 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).
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 91 2 52 3 53 1 103 4 124 1 84 6 115 4 75 6 96 5 46 51 2 12 3 13 4 14 5 15 6 1
Sample Output
42-1HintIn the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.
Author
RoBa@TJU
Source
HDU 2007 Programming Contest - Final
题意:给一个有向图,问每个点都只在一个简单环上,可以有多个环,边权总和最小为多少,如果不满足条件就输出-1。
#include<stdio.h>#include<string.h>#include<queue>using namespace std;const int MAXN = 10010;const int MAXM = 100100;const int INF = 1<<30;struct EDG{ int to,next,cap,flow; int cost; //每条边的单价}edg[MAXM];int head[MAXN],eid;int pre[MAXN], cost[MAXN] ; //点0~(n-1)void init(){ eid=0; memset(head,-1,sizeof(head));}void addEdg(int u,int v,int cap,int cst){ edg[eid].to=v; edg[eid].next=head[u]; edg[eid].cost = cst; edg[eid].cap=cap; edg[eid].flow=0; head[u]=eid++; edg[eid].to=u; edg[eid].next=head[v]; edg[eid].cost = -cst; edg[eid].cap=0; edg[eid].flow=0; head[v]=eid++;}bool inq[MAXN];bool spfa(int sNode,int eNode,int n){ queue<int>q; for(int i=0; i<n; i++){ inq[i]=false; cost[i]= INF; } cost[sNode]=0; inq[sNode]=1; pre[sNode]=-1; q.push(sNode); while(!q.empty()){ int u=q.front(); q.pop(); inq[u]=0; for(int i=head[u]; i!=-1; i=edg[i].next){ int v=edg[i].to; if(edg[i].cap-edg[i].flow>0 && cost[v]>cost[u]+edg[i].cost){ //在满足可增流的情况下,最小花费 cost[v] = cost[u]+edg[i].cost; pre[v]=i; //记录路径上的边 if(!inq[v]) q.push(v),inq[v]=1; } } } return cost[eNode]!=INF; //判断有没有增广路}//反回的是最大流,最小花费为minCostint minCost_maxFlow(int sNode,int eNode ,int& minCost,int n){ int ans=0; while(spfa(sNode,eNode,n)){ ans++; for(int i=pre[eNode]; i!=-1; i=pre[edg[i^1].to]){ edg[i].flow+=1; edg[i^1].flow-=1; minCost+=edg[i].cost; } } return ans;}void scanf(int &ans){ char ch; while(ch=getchar()){ if(ch>='0'&&ch<='9') break; } ans=ch-'0'; while(ch=getchar()){ if(ch<'0'||ch>'9') break; ans=ans*10+ch-'0'; }}int mapt[1005][1005];int main(){ int n,m , u, v, d ; while(scanf("%d%d",&n,&m)>0){ init(); int s=0, t=2*n+1; for(int i=1; i<=n; i++){ addEdg(s , i , 1 , 0); addEdg(i+n , t , 1 , 0); for(int j=1; j<=n; j++) mapt[i][j]=INF; } while(m--){ scanf(u); scanf(v); scanf(d); if(mapt[u][v]>d) mapt[u][v]=d; } for( u=1; u<=n; u++) for(v=1; v<=n; v++) if(mapt[u][v]!=INF) addEdg(u,v+n,1,mapt[u][v]); int mincost=0; n-= minCost_maxFlow(s , t , mincost , t+1); if(n==0) printf("%d\n",mincost); else printf("-1\n"); }}
0 0
- 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 1853 Cyclic Tour(最小费用流)
- HDU 1853 Cyclic Tour (最小费用最大流+环的判断)
- hdu 1853 Cyclic Tour(最小权匹配/费用流)
- HDU 1853 Cyclic Tour(最小费用流)
- HDU 1853 Cyclic Tour && HDU 3488Tour 最小费用流
- HDU 1853--Cyclic Tour【最小费用最大流 && 有向环最小权值覆盖 】
- HDU 1853 Cyclic Tour【最小费用最大流 OR 带权二分图匹配】
- Cyclic Tour hdu 1853 最小费用最最大流
- HDU 1853Cyclic Tour(网络流之最小费用流)
- HDU 1853 Cyclic Tour //费用流
- 动画操作
- linux程序设计——执行SQL语句(第八章)
- 病人监护、胎儿监护(三参、九参)全套方案
- 移动前端不得不了解的html5 head 头标签
- 翻转二叉树(C++)
- HDU 1853 Cyclic Tour(最小费用最大流)
- iOS类别和扩展(category和extension)
- 实现链式队列
- adt Failed to create the Java Virtual Machine.
- 30. Socket下载时,服务器端出错: len=10240 count=115 len=10240 count=116 len=7168 count=last java.net.SocketExc
- code vs1099 字串变化 字符串搜索(STL)
- [CSS]IE6和360浏览器兼容模式下的兼容性问题
- Python初始化实例属性
- 钱咖是什么?钱咖怎么赚钱?钱咖是真的吗?钱咖能提现吗?钱咖安全吗?