HDU 4411 费用流,诡异的构图

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. 

题意:

自己看吧。

代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 10010;
const int MAXM = 100010;
const int INF = 0x3f3f3f3f;
const int ff=100000;
int sumFlow;
struct Edge
{
    int u,v,cap,cost,nex;
}edge[MAXM];
int e_max;
int fir[MAXN], dist[MAXN], pre[MAXN];
bool vis[MAXN];
int q[9999999];
inline void init()
{
    e_max=0;
    memset(fir,-1,sizeof(fir));
}
inline void add_edge(int u,int v,int cap,int cost)
{
    edge[e_max].u=u;edge[e_max].v=v;edge[e_max].cap=cap;edge[e_max].cost=cost;
    edge[e_max].nex=fir[u];fir[u]=e_max++;
    edge[e_max].u=v;edge[e_max].v=u;edge[e_max].cap=0;edge[e_max].cost=-cost;
    edge[e_max].nex=fir[v];fir[v]=e_max++;
}
bool SPFA(int s,int t)
{
    int i,u,v;
    memset(vis,false,sizeof vis );
    memset(pre,-1,sizeof pre );
    memset(dist,INF,sizeof dist);
    vis[s]=true;
    dist[s]=0;
    q[0]=s;
    int f=0,r=1;
    while(f<r)
    {
        u=q[f++];
        vis[u]=false;
        for(i=fir[u];i!=-1;i=edge[i].nex)
        {
            v=edge[i].v;
            if(edge[i].cap&&dist[v]>dist[u]+edge[i].cost)
            {
                dist[v]=dist[u]+edge[i].cost;
                pre[v]=i;
                if(!vis[v])
                {
                        q[r++]=v;
                        vis[v]=true;
                }
            }
        }
    }
    if(dist[t]==INF)
        return false;
    return true;
}
int ford_fulkerson(int s,int t)
{
    int flow=0; // 总流量
    int i,minflow,mincost;
    mincost=0;
    while(SPFA(s,t))
    {
        minflow=INF+1;
        for(i=pre[t];i!=-1;i=pre[edge[i].u])
            if(edge[i].cap<minflow)
                minflow=edge[i].cap;
        flow+=minflow;
        for(i=pre[t];i!=-1;i=pre[edge[i].u])
        {
            edge[i].cap-=minflow;
            edge[i^1].cap+=minflow;
        }
        mincost+=dist[t]*minflow;
    }
    sumFlow=flow; // 最大流
    return mincost;
}

int d[200][200];
int main()
{
    int S,T;
    int n,m,k;
    int u,v,f,c;
    while(scanf("%d%d%d",&n,&m,&k)!=-1)
    {
        if(n==0&&m==0)
            break;
        for(int i=0; i<=n; i++)
        {
            for(int j=0; j<=n; j++)
                d[i][j]=(i==j)?0:INF;
        }
        S=n*2+1,T=n*2+2;
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&u,&v,&c);
            d[u][v]=d[v][u]=min(d[u][v],c);
        }
        for(int i=0; i<=n; i++)
            for(int j=0; j<=n; j++)
                for(int k=0; k<=n; k++)
                    if(d[j][i]<INF&&d[i][k]<INF&&d[j][k]>d[j][i]+d[i][k])
                        d[j][k]=d[j][i]+d[i][k];
        init();
        add_edge(S,0,k,0);
        add_edge(0,T,k,0);
        for(int i=1; i<=n; i++)
        {
            add_edge(0,i,1,d[0][i]);
            add_edge(i,i+n,1,-ff);
            add_edge(i+n,T,1,d[0][i]);
        }
        for(int i=1; i<n; i++)
            for(int j=i+1; j<=n; j++)
                add_edge(i+n,j,1,d[i][j]);

        cout<<ford_fulkerson(S,T)+n*ff<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值