hdu4411Arrest【最小费用最大流 拆点】

Status

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 2 0 1 3 0 2 4 1 3 2 2 3 2 0 0 0
 

Sample Output

     
     
14
 

题意:警察们从0点出发逮捕1-n的犯人,但是i节点逮捕前必须逮捕i-1的,最后警察们还的全回到0点。问最少走的总距离

最开始就想到了应该是让点0/1作为流量,w是费用,回来的时候加上dist[],方向终于对了一次,但是,首先,涉及点权就一定要拆点,加边的值是点权,可是拆开的两点的费用呢?我们让他是100000,最后得的值加上n*100000就好了啊(这样做的也是限制了每个点只取一次)。而且,你既然想到回来的时候要用dist,那每次从某点到某点时候也应该走最短路啊~0点前面还的加超级源点,因为要限制k

具体实现见代码

#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
int n,m,k;
const int mm=66666;
const int mn=5555;
const int oo=1e9;
int src,dest,node,edge;
int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
int ver[mm],cost[mm],flow[mm],nxt[mm];
int head[mn],dis[mn],p[mn],q[mn];
int h[55][55];
bool vis[mn]={0};
void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0;i<node;++i)head[i]=-1;
    edge=0;
}
void addedge(int u,int v,int f,int c)
{
    ver[edge]=v,flow[edge]=f,cost[edge]=c,nxt[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,cost[edge]=-c,nxt[edge]=head[v],head[v]=edge++;
}
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=nxt[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,delta,ret=0;
    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 dist[110][110];
int main()
{
    //freopen("cin.txt","r",stdin);
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        if(n==0&&m==0&&k==0) break;
        prepare(2*n+3,2*n+1,2*n+2);
        memset(dist,0x3f3f3f3f,sizeof(dist));
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            if(w<dist[u][v])
            dist[u][v]=dist[v][u]=w;
        }
        for(int i=0;i<=n;i++)
        for(int j=0;j<=n;j++)
        for(int k=0;k<=n;k++)
            if(dist[j][i]+dist[i][k]<dist[j][k])
            dist[j][k]=dist[j][i]+dist[i][k];
        addedge(src,0,k,0);
        addedge(0,dest,k,0);
        for(int i=1;i<=n;i++)
        {
            addedge(0,i,1,dist[0][i]);
            addedge(i,i+n,1,-100000);
            addedge(i+n,dest,1,dist[i][0]);
        }
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++)
                addedge(i+n,j,1,dist[i][j]);
        }
        printf("%d\n",-Spfaflow()+n*100000);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值