hdu4411(最小费用流)
来源:互联网 发布:手机淘宝好评在哪里 编辑:程序博客网 时间:2024/05/10 05:33
Arrest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1742 Accepted Submission(s): 691
Problem Description
There are (N+1) cities on TAT island. City 0 is where police headquarter located. The economy of other cities numbered from 1 to N ruined these years because they are all controlled by mafia. The police plan to catch all the mafia gangs in these N cities all over the year, and they want to succeed in a single mission. They figure out that every city except city 0 lives a mafia gang, and these gangs have a simple urgent message network: if the gang in city i (i>1) is captured, it will send an urgent message to the gang in city i-1 and the gang in city i -1 will get the message immediately.
The mission must be carried out very carefully. Once a gang received an urgent message, the mission will be claimed failed.
You are given the map of TAT island which is an undirected graph. The node on the graph represents a city, and the weighted edge represents a road between two cities(the weight means the length). Police headquarter has sent k squads to arrest all the mafia gangs in the rest N cities. When a squad passes a city, it can choose to arrest the gang in the city or to do nothing. These squads should return to city 0 after the arrest mission.
You should ensure the mission to be successful, and then minimize the total length of these squads traveled.
The mission must be carried out very carefully. Once a gang received an urgent message, the mission will be claimed failed.
You are given the map of TAT island which is an undirected graph. The node on the graph represents a city, and the weighted edge represents a road between two cities(the weight means the length). Police headquarter has sent k squads to arrest all the mafia gangs in the rest N cities. When a squad passes a city, it can choose to arrest the gang in the city or to do nothing. These squads should return to city 0 after the arrest mission.
You should ensure the mission to be successful, and then minimize the total length of these squads traveled.
Input
There are multiple test cases.
Each test case begins with a line with three integers N, M and k, here M is the number of roads among N+1 cities. Then, there are M lines. Each of these lines contains three integers X, Y, Len, which represents a Len kilometer road between city X and city Y. Those cities including city 0 are connected.
The input is ended by “0 0 0”.
Restrictions: 1 ≤ N ≤ 100, 1 ≤ M ≤ 4000, 1 ≤ k ≤ 25, 0 ≤ Len ≤ 1000
Each test case begins with a line with three integers N, M and k, here M is the number of roads among N+1 cities. Then, there are M lines. Each of these lines contains three integers X, Y, Len, which represents a Len kilometer road between city X and city Y. Those cities including city 0 are connected.
The input is ended by “0 0 0”.
Restrictions: 1 ≤ N ≤ 100, 1 ≤ M ≤ 4000, 1 ≤ k ≤ 25, 0 ≤ Len ≤ 1000
Output
For each test case,output a single line with a single integer that represents the minimum total length of these squads traveled.
Sample Input
3 4 20 1 30 2 41 3 22 3 20 0 0
Sample Output
14
题意:给出一个图,n+1个点(包括0),m条边,有k个警察,遍历点的时候如果到i点抓了犯人,i-1点的犯人就会逃跑,任务就失败,现在让你求抓到所有犯人需要的最小费用。
思路:由于题目数据给的比较小,最多100个点,用floyd处理出来每两个点的最短路,然后拆点,建立源点和汇点,源点连接0,路程k(由于最多k个人),费用0。然后0连接汇点,路程k,费用0。然后就是中间的连边处理,从0出发的,所以连接0到n个点,路程为1,费用为0到i点的最短路。接着拆点,为了经过每个点,连接i和i+n,路程1,费用-100000。接着连接i+n到j(i<j,题意所迫)路程1,费用为i到j的最短路。最后,i+n到汇点建边,路程1,费用为i到0的最短路。跑完费用流之后用结果加n*100000,因为把两两个拆的点之间的费用设为了最小,肯定会经过这n个-100000,所以结果再加上来就ok!
ac代码:
#include <iostream>#include <stdio.h>#include <stdlib.h>#include<string.h>#include<algorithm>#include<math.h>#include<queue>#include<iomanip>using namespace std;typedef long long ll;const int oo=1e9;const int mm=11111111;const int mn=888888;int node,src,dest,edge;int ver[mm],flow[mm],cost[mm],nex[mm];int head[mn],dis[mn],p[mn],q[mn],vis[mn];/**这些变量基本与最大流相同,增加了 cost 表示边的费用, p 记录可行流上节点对应的反向边 */void prepare(int _node,int _src,int _dest){ node=_node,src=_src,dest=_dest; for(int i=0; i<node; i++)head[i]=-1,vis[i]=0; edge=0;}void addedge(int u,int v,int f,int c){ ver[edge]=v,flow[edge]=f,cost[edge]=c,nex[edge]=head[u],head[u]=edge++; ver[edge]=u,flow[edge]=0,cost[edge]=-c,nex[edge]=head[v],head[v]=edge++;}/**以上同最大流*//**spfa 求最短路,并用 p 记录最短路上的边*/bool spfa(){ int i,u,v,l,r=0,tmp; for(i=0; i<node; ++i)dis[i]=oo; dis[q[r++]=src]=0; p[src]=p[dest]=-1; for(l=0; l!=r; (++l>=mn)?l=0:l) for(i=head[u=q[l]],vis[u]=0; i>=0; i=nex[i]) if(flow[i]&&dis[v=ver[i]]>(tmp=dis[u]+cost[i])) { dis[v]=tmp; p[v]=i^1; if(vis[v]) continue; vis[q[r++]=v]=1; if(r>=mn)r=0; } return p[dest]>-1;}/**源点到汇点的一条最短路即可行流,不断的找这样的可行流*/int SpfaFlow(){ int i,ret=0,delta; while(spfa()) { for(i=p[dest],delta=oo; i>=0; i=p[ver[i]]) if(flow[i^1]<delta)delta=flow[i^1]; for(i=p[dest]; i>=0; i=p[ver[i]]) flow[i]+=delta,flow[i^1]-=delta; ret+=delta*dis[dest]; } return ret;}int tu[110][110];void floyd(int n){ for(int k=0; k<=n; k++) for(int i=0; i<=n; i++) for(int j=0; j<=n; j++) tu[i][j]=min(tu[i][j],tu[i][k]+tu[k][j]);}int main(){ int n,m,k; while(~scanf("%d%d%d",&n,&m,&k)&&n+m+k) { int st=2*n+1,ed=2*n+2; prepare(2*n+3,st,ed); for(int i=0; i<=n; i++) for(int j=0; j<=n; j++) tu[i][j]=i==j?0:oo; while(m--) { int a,b,c; scanf("%d%d%d",&a,&b,&c); if(c<tu[a][b]) tu[a][b]=tu[b][a]=c; } floyd(n); addedge(st,0,k,0); addedge(0,ed,k,0); for(int i=1; i<=n; i++) addedge(0,i,1,tu[0][i]),addedge(i,i+n,1,-100000),addedge(i+n,ed,1,tu[i][0]); for(int i=1; i<=n; i++) for(int j=i+1; j<=n; j++) addedge(i+n,j,1,tu[i][j]); printf("%d\n",SpfaFlow()+100000*n); } return 0;}
0 0
- hdu4411(最小费用流)
- hdu4411(#最小费用流)
- hdu4411--最小费用最大流
- hdu4411 Arrest 最小费用流
- hdu4411(费用流)
- hdu4411 经典费用里建图
- 最小(大)费用流
- HDU3395(最小费用流)
- hdu3526(最小费用流)
- poj3422(*最小费用流)
- poj3680(*最小费用流)
- hdu4780(*最小费用流)
- hdu4322(*最小费用流)
- 费用流模板 (最大最小费用流)
- 最小费用最小流uva10746
- poj 2159(最小费用最大流)
- POJ 2516(最小费用最大流)
- Transportation 最小费用流 hdu3667 (拆边)
- STM32系列第20篇--DAC
- bell-manford判负环模板
- Net分布式系统之一:系统整体框架介绍
- C++实验3-定期存款利息计算器
- 2016华为挑战赛_寻路_赛题分析
- hdu4411(最小费用流)
- AsynTask源码总结
- 博客首发
- solr入门之数据源处理工具类去除字段中括号内内容,字母,数字及特殊符号
- 分享某公司JAVA实习生招聘代码面的一道题
- java集合框架源码学习目录(持续更新中)
- Ubuntu14.10下LAMP的环境配置
- TransactionProxyFactoryBean 代理事务 $Proxy9 cannot be cast to异常解决方法
- NDK实现的加密/解密库-Codec