HDU 4411 Arrest

ProblemDescription

There are (N+1)cities on TAT island. City 0 is where police headquarter located. The economyof other cities numbered from 1 to N ruined these years because they are allcontrolled by mafia. The police plan to catch all the mafia gangs in these Ncities all over the year, and they want to succeed in a single mission. Theyfigure out that every city except city 0 lives a mafia gang, and these gangshave a simple urgent message network: if the gang in city i (i>1) iscaptured, it will send an urgent message to the gang in city i-1 and the gangin city i -1 will get the message immediately. 
The mission must be carried out very carefully. Once a gang received an urgentmessage, the mission will be claimed failed.
You are given the map of TAT island which is an undirected graph. The node onthe graph represents a city, and the weighted edge represents a road betweentwo cities(the weight means the length). Police headquarter has sent k squadsto 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 squadsshould return to city 0 after the arrest mission.
You should ensure the mission to be successful, and then minimize the totallength of these squads traveled.

 

 

Input

There are multipletest cases.
Each test case begins with a line with three integers N, M and k, here M is thenumber of roads among N+1 cities. Then, there are M lines. Each of these linescontains three integers X, Y, Len, which represents a Len kilometer roadbetween 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 testcase,output a single line with a single integer that represents the minimumtotal 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

 集训期间,实在太累,不写了。。。。。。待到开学再来慢慢写吧

 

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define INF 1000000000LL
using namespace std;
const long long M = 10 * 10010;//边

struct Node//边,点f到点t,流量为c,费用为w
{
	int st, ed;
	long long flow, cost;
	int next;
}edge[M];

long long head[M], dis[M], q[M], pre[M], cnt;//cnt为已添加的边数,head为邻接表,dis为花费,pre为父亲节点
bool vis[M];
int n, m, k;//n个点,m条路
long long dist[110][110] ,s ,t;

void init()
{
	memset(head, -1, sizeof(head));
	cnt = 0;
}

void add_edge(int f, int t, long long d1, long long d2, long long w)
{//f到t的一条边,流量为d1,反向流量d2,花费w,反向边花费-w(可以反悔)
	edge[cnt].st = f;
	edge[cnt].ed = t;
	edge[cnt].flow = d1;
	edge[cnt].cost = w;
	edge[cnt].next = head[f];
	head[f] = cnt++;

	edge[cnt].st = t;
	edge[cnt].ed = f;
	edge[cnt].flow = d2;
	edge[cnt].cost = -w;
	edge[cnt].next = head[t];
	head[t] = cnt++;
}

bool spfa(int s, int t, int n)
{
	long long  tmp, l, r;
	int i;
	memset(pre, -1, sizeof(pre));
	for(i = 0; i < n; ++i)
    {
        dis[i] = INF;
    }
	dis[s] = 0;
	q[0] = s;
	l = 0, r = 1;
	vis[s] = true;
	while(l != r)
    {
		tmp = q[l];
		l = (l + 1) % (n + 1);
		vis[tmp] = false;
		for(i = head[tmp]; i!=-1; i = edge[i].next)
		{
			if(edge[i].flow && dis[edge[i].ed] > dis[tmp] + edge[i].cost)
			{
				dis[edge[i].ed] = dis[tmp] + edge[i].cost;
				pre[edge[i].ed] = i;
				if(!vis[edge[i].ed])
				{
					vis[edge[i].ed] = true;
					q[r] = edge[i].ed;
					r = (r + 1) % (n + 1);
				}
			}
		}
	}
	if(pre[t] == -1)
	{
	    return false;
	}
	return true;
}

void MCMF(int s, int t, int n, long long &flow, long long &cost)
{//起点s,终点t,点数n,最大流flow,最小花费cost
	long long tmp, arg;
	flow = cost = 0;
	while(spfa(s, t, n))
    {
		arg = INF;
		tmp = t;
		while(tmp != s)
		{
			if(arg > edge[pre[tmp]].flow)
			{
				arg = edge[pre[tmp]].flow;
			}
			tmp = edge[pre[tmp]].st;
		}
		tmp = t;
		while(tmp != s)
		{
			edge[pre[tmp]].flow -= arg;
			edge[pre[tmp] ^ 1].flow += arg;
			tmp = edge[pre[tmp]].st;
		}
		flow += arg;
		cost += arg * dis[t];
	}
}

int main()
{
    int x ,y ,len;
    long long flow ,cost;
    while(~scanf("%d%d%d",&n,&m,&k),n+m+k!=0)
    {
        for(int i = 0;i<=n;i++)
        {
            for(int j = 0;j<=n;j++)
            {
                dist[i][j] = INF;
            }
            dist[i][i] = 0;
        }
        for(int i = 0;i<m;i++)
        {
            scanf("%d%d%d",&x,&y,&len);
            if(dist[x][y] > len)
            {
                dist[x][y] = dist[y][x] = len;
            }
        }
        for(int i = 0;i<=n;i++)
        {
            for(int j = 0;j<=n;j++)
            {
                for(int k = 0;k<=n;k++)
                {
                    dist[j][k] = min(dist[j][k],dist[j][i] + dist[i][k]);
                }
            }
        }
        init();
        s = 0;
        add_edge(0,1,k,0,0);
        for(int j = 1;j<=n;j++)
        {
            add_edge(1,j * 2,INF,0,dist[0][j]);
        }
        for(int i = 1;i<=n;i++)
        {
            add_edge(i * 2,i * 2 + 1,1,0,-INF);
            for(int j = i + 1;j<=n;j++)
            {
                add_edge(i * 2 + 1,j * 2,INF,0,dist[i][j]);
            }
        }
        t = 2 * (n + 1);
        for(int j = 1;j<=n;j++)
        {
            add_edge(j * 2 + 1,t,INF,0,dist[0][j]);
        }
        add_edge(1,t,k,0,0);
        MCMF(s,t,t+1,flow,cost);
        cost += n * INF;
        printf("%lld\n",cost);
    }
    return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值