hdoj 4411 Arrest 【费用流 + 最短路】

来源:互联网 发布:数据分析师笔试逻辑 编辑:程序博客网 时间:2024/06/03 17:14

Arrest

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


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 20 1 30 2 41 3 22 3 20 0 0
 

Sample Output
14
 

题意:有n+1个城市(0 - n)和m条无向边。已知在1 - n城市均有一个盗贼,在城市0有k个警察。现在去抓这n个盗贼,要求抓第i个盗贼前必须先抓到第i-1个,且抓完n个盗贼后要返回城市0。问抓到所有盗贼警察们需要走的最小路程和。


思路:明显的费用流。预处理下最短路。建边时拆点就可以了,因为需要的是最小费用,每次找增广路找的是最小花费路,那么把边的费用设为-INF就可以了。

建边:源点到0引容量为k、费用0的边。1-n拆点i - i+n。i+n -> j建边(i < j)。因为需要返回i+n -> 汇点建边。

注意:k个警察一起去抓不一定是最优解,这点0到汇点建容量为k、费用为0的边就可以了。


AC代码:


#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#include <string>#define INF 1000000#define eps 1e-8#define MAXN (200+10)#define MAXM (100000+10)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while((a)--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1#define PI acos(-1.0)#pragma comment(linker, "/STACK:102400000,102400000")#define fi first#define se secondusing namespace std;struct Edge{    int from, to, cap, flow, cost, next;};Edge edge[MAXM];int head[MAXN], edgenum;int dist[MAXN], pre[MAXN];bool vis[MAXN];void init(){CLR(head, -1), edgenum = 0;}void addEdge(int u, int v, int w, int c){    Edge E1 = {u, v, w, 0, c, head[u]};    edge[edgenum] = E1;    head[u] = edgenum++;    Edge E2 = {v, u, 0, 0, -c, head[v]};    edge[edgenum] = E2;    head[v] = edgenum++;}bool BFS(int s, int t){    queue<int> Q;    CLR(pre, -1); CLR(dist, INF); dist[s] = 0; CLR(vis, false); 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].to;            if(dist[v] > dist[u] + edge[i].cost && edge[i].cap > edge[i].flow)            {                dist[v] = dist[u] + edge[i].cost; pre[v] = i;                if(!vis[v])                {                    vis[v] = true;                    Q.push(v);                }            }        }    }    return pre[t] != -1;}void MCMF(int s, int t, int &cost, int &flow){    cost = flow = 0;    while(BFS(s, t))    {        int Min = INF;        for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])            Min = min(Min, edge[i].cap - edge[i].flow);        for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])        {            edge[i].flow += Min;            edge[i^1].flow -= Min;            cost += edge[i].cost * Min;        }        flow += Min;    }}int Map[MAXN][MAXN];void Floyd(int n){    for(int k = 0; k <= n; k++)        for(int i = 0; i <= n; i++)            for(int j = 0; j <= n; j++)                Map[i][j] = min(Map[i][k] + Map[k][j], Map[i][j]);}int main(){    int n, m, k;    while(scanf("%d%d%d", &n, &m, &k) != EOF)    {        if(n == 0 && m == 0 && k == 0) break;        for(int i = 0; i <= n; i++)            for(int j = 0; j <= n; j++)                Map[i][j] = (i == j) ? 0 : INF;        for(int i = 1; i <= m; i++)        {            int x, y, z;            Ri(x); Ri(y); Ri(z);            if(Map[x][y] > z)                Map[x][y] = Map[y][x] = z;        }        Floyd(n); int s = 2*n+1, t = 2*n+2; //int ans = INF;        init();        for(int i = 1; i <= n; i++)        {            addEdge(i, i+n, 1, -INF);            if(Map[0][i] != INF) {addEdge(0, i, INF, Map[0][i]); addEdge(i+n, t, INF, Map[0][i]);}            for(int j = i+1; j <= n; j++) if(Map[i][j] != INF)                addEdge(i+n, j, 1, Map[i][j]);        }        addEdge(s, 0, k, 0); addEdge(0, t, k, 0);        int cost, flow;        MCMF(s, t, cost, flow);        Pi(cost + n * INF);    }    return 0;}


0 0
原创粉丝点击