HDU 4411 Arrest 费用流

来源:互联网 发布:淘宝店铺一心好做吗 编辑:程序博客网 时间:2024/05/22 00:12

题目描述:

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.

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

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市为警察局所在城市其中有k个警察,在其他n个城市中,均有小偷。有m条路,每条路有其特定路程,求k个警察将n个城市的小偷全抓捕到0市的最短路程。其中抓小偷必须从1、2、3、、、n按照城市号码严格递增顺序抓。
最小费用流的问题。样例输出解释:0->1->3(不抓小偷)->2->3->1->0。路程和为3+2+2+2+2+3=14。
设置超级源点s,超级汇点t。将0点与s和t建立容量为k,费用为0的边,并将其他n点拆掉。n个点与0点建立容量1费用为两点最短路的边,同时n个被拆出来的点与t建立容量为1费用为两点最短路的边,n个点与其被拆出来的点建立容量为1费用足够小的边(足够小的意思是保证不会出现s->0->t),跑一边最小费用,最后输出的时候把n个足够小费用加回去。

代码如下:

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;typedef long long ll;const int MINC = -1000000;//适当小的值const int INF = 0x3f3f3f3f;const int MAXN = 220;const int MAXM = 40040;struct node{    int u,v,cap,flow,cost,next;}edge[MAXM];int map[MAXN][MAXN];int num;int n,m,k;int head[MAXN];int id;int pre[MAXN],dis[MAXN];bool vis[MAXN];int s,t;void init(){    id=0;    memset(head,-1,sizeof(head));}void addedge(int u, int v, int cap, int cost){    edge[id].u=u;    edge[id].v=v;    edge[id].cap=cap;    edge[id].flow=0;    edge[id].cost=cost;    edge[id].next=head[u];    head[u]=id++;    edge[id].u=v;    edge[id].v=u;    edge[id].cap=0;    edge[id].flow=0;    edge[id].cost=-cost;    edge[id].next=head[v];    head[v]=id++;}bool spfa(int s,int t){    queue<int>q;    memset(dis,INF,sizeof(dis));    memset(vis,false,sizeof(vis));    memset(pre,-1,sizeof(pre));    dis[s]=0;    vis[s]=true;    q.push(s);    while(!q.empty())    {        int u=q.front();        q.pop();        vis[u]=false;        for(int i=head[u]; i!=-1; i=edge[i].next)        {            int v=edge[i].v;            if(edge[i].cap > edge[i].flow && dis[v] > dis[u]+edge[i].cost)            {                dis[v]=dis[u]+edge[i].cost;                pre[v]=i;                if(!vis[v])                {                    vis[v]=true;                    q.push(v);                }            }        }    }    if (pre[t]==-1) return false;    else return true;}int minCostMaxflow(int s,int t)//本题是寻找最小费用,因此返回cost{    int flow=0,cost=0;    while(spfa(s,t))    {        int Min=INF;        for(int i=pre[t]; i!=-1; i=pre[edge[i^1].v])        {            Min=min(edge[i].cap-edge[i].flow,Min);        }        for(int i=pre[t]; i!=-1; i=pre[edge[i^1].v])        {            edge[i].flow+=Min;            edge[i^1].flow-=Min;            cost+=edge[i].cost*Min;        }        flow+=Min;    }    //return flow;    return cost;}int main(){    while(~scanf("%d%d%d",&n,&m,&k) && (n || m || k))    {        init();        s=n*2+1;        t=s+1;        for(int i=0; i<=n; i++)          for(int j=0; j<=n; j++)            if (i!=j) map[i][j]=INF;            else map[i][j]=0;        while(m--)        {            int u,v,w;            scanf("%d%d%d",&u,&v,&w);            map[u][v]=map[v][u]=min(w,map[u][v]);        }        for(int l=0;l<=n;l++)//Floyd求最短路           for(int i=0;i<=n;i++)               for(int j=0;j<=n;j++)                   if(map[i][l]<INF && map[l][j]<INF && map[i][l]+map[l][j]<map[i][j])                      map[i][j]=map[i][l]+map[l][j];        addedge(s,0,k,0);//源点与0点,容量为k,费用为0        addedge(0,t,k,0);//汇点与0点,容量为k,费用为0        for(int i=1; i<=n; i++)        {            for(int j=i+1; j<=n; j++)            {                addedge(i+n,j,1,map[i][j]);//先到i点再到j点            }            addedge(0,i,1,map[0][i]);//0点到i点,容量为1,费用为最短路            addedge(i,i+n,1,MINC);//拆点,cost值设为适当小的值,以免导致s->0->t            addedge(i+n,t,1,map[0][i]);//i点到0点,容量为1,费用为最短路        }        printf("%d\n",minCostMaxflow(s,t)-MINC*n);    }    return 0;}
0 0
原创粉丝点击