1. 程式人生 > >HDU 4411 Arrest 費用流之可行流 巧妙構圖

HDU 4411 Arrest 費用流之可行流 巧妙構圖

                                             Arrest

                Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)                  Total Submission(s): 2123    Accepted Submission(s): 848  

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.

 

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

 

Source

 

Recommend

liuyiding

題意: 從0出發有k個警衛去抓n個城市的壞人(並且需要回到0),但是隻能先抓掉n之前的,才能抓第n個,求總的路徑長度最小為多少。

建圖:

g[i][j] 為i->j之間的最短路,可以先用floyd先處理一下

s = 2*n+1, t = 2*n + 2

s -> 0  cap = k, cost = 0                                                                              0 -> i     cap = 1  cost = g[0][i];

0 -> t   cap = k, cost = 0(是流量平衡,不一定非得跑滿k最大流)                i+n -> t  cap = 1  cost = g[i][0];

i  -> i+n cap = 1, cost = -1e6    (貪心,讓其先選擇走這條路,避免直接走 s-0-t )

i  -> j  ( j > i)  cap = 1, cost = g[i][j]

#include <bits/stdc++.h>

using namespace std;

const int maxn = 200+100;
const int maxm = 100000+100;
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int,int> P;
const LL mod = 1e9 + 7;



struct edge
{
   int from, to, cap, flow, cost;
   edge(int from, int to, int cap, int flow, int cost) : from(from), to(to), cap(cap), flow(flow), cost(cost) {}
};


int tot = 0;
vector <edge> E;

vector <int > G[maxn];

int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn];

void add_edge(int from, int to, int cap, int cost)
{
	E.push_back(edge(from,to,cap,0,cost));
	E.push_back(edge(to,from,0,0,-cost));
	tot = E.size();
	G[from].push_back(tot-2);
	G[to].push_back(tot-1);
}

bool spfa(int s, int t, int &flow, int &cost)
{
    memset(d,INF,sizeof(d));
    memset(inq,0,sizeof(inq));
    d[s] = 0;inq[s]= 1;p[s]= 0;a[s] = INF;
    queue<int> Q;
    Q.push(s);
    while(!Q.empty())
    {
	int u = Q.front();
	Q.pop();
	inq[u] = 0;
       for(int i = 0; i < G[u].size(); i++)
       {
	    edge e = E[G[u][i]];
	    if(e.cap > e.flow && d[e.to] > d[u] + e.cost)
	    {
		 d[e.to] = d[u] + e.cost;
		 p[e.to] = G[u][i];
                 a[e.to] = min(a[u],e.cap-e.flow);
		 if(!inq[e.to]) {Q.push(e.to);inq[e.to] = 1;}
	    }
      }

    }
    if(d[t] == INF) return false;
    flow += a[t];
    cost += d[t]*a[t];
    int u = t;
    while(u != s)
    {
        E[p[u]].flow += a[t];
        E[p[u]^1].flow -= a[t];
        u = E[p[u]].from;
    }	
}

int mcf(int s, int t)
{
   int flow = 0,cost = 0;
   while(spfa(s,t,flow,cost));
   return cost;
}


int g[maxn][maxn];


int main()
{
   int n,m,k;
   while(scanf("%d%d%d",&n,&m,&k) && n && m && k)
   {
   E.clear();   
   int s = 2*n+1,t = 2*n+2;
   for(int i= 0; i <=t ; i++) G[i].clear();
   add_edge(s,0,k,0);
   add_edge(0,t,k,0);
   memset(g,INF,sizeof(g));
   for(int i = 0; i < m; i++) 
     {
	     int u,v,c;
	     scanf("%d%d%d",&u,&v,&c);
	     g[u][v] = g[v][u] = min(g[u][v],c);
     }
   for(int i = 0; i <= n; i++)
	   for(int j = 0; j <= n; j++)
		   for(int k = 0; k <= n; k++)
			    g[k][j] = g[j][k] = min(g[j][k],g[j][i]+g[i][k]);
   for(int i = 1; i <= n; i++)
   {
	   for(int j = i+1; j <= n; j++)
	   {
		   add_edge(i+n,j,1,g[i][j]);
//		   cout << i << " " << j << " "<< g[i][j] << endl;
	   }
	   add_edge(0,i,1,g[0][i]);
	   add_edge(i+n,t,1,g[i][0]);
	   add_edge(i,i+n,1,-1000000);
//	   cout << g[0][i] << endl;
   }
   printf("%d\n",mcf(s,t) + n*1000000);
   }
   return 0;
}