POJ3068 "Shortest" pair of paths

来源:互联网 发布:jsp引入java 编辑:程序博客网 时间:2024/06/01 09:23

"Shortest" pair of paths
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 1385 Accepted: 609

Description

A chemical company has an unusual shortest path problem. 

There are N depots (vertices) where chemicals can be stored. There are M individual shipping methods (edges) connecting pairs of depots. Each individual shipping method has a cost. In the usual problem, the company would need to find a way to route a single shipment from the first depot (0) to the last (N - 1). That's easy. The problem they have seems harder. They have to ship two chemicals from the first depot (0) to the last (N - 1). The chemicals are dangerous and cannot safely be placed together. The regulations say the company cannot use the same shipping method for both chemicals. Further, the company cannot place the two chemicals in same depot (for any length of time) without special storage handling --- available only at the first and last depots. To begin, they need to know if it's possible to ship both chemicals under these constraints. Next, they need to find the least cost of shipping both chemicals from first depot to the last depot. In brief, they need two completely separate paths (from the first depot to the last) where the overall cost of both is minimal. 

Your program must simply determine the minimum cost or, if it's not possible, conclusively state that the shipment cannot be made.

Input

The input will consist of multiple cases. The first line of each input will contain N and M where N is the number of depots and M is the number of individual shipping methods. You may assume that N is less than 64 and that M is less than 10000. The next M lines will contain three values, i, j, and v. Each line corresponds a single, unique shipping method. The values i and j are the indices of two depots, and v is the cost of getting from i to j. Note that these shipping methods are directed. If something can be shipped from i to j with cost 10, that says nothing about shipping from j to i. Also, there may be more than one way to ship between any pair of depots, and that may be important here. 
A line containing two zeroes signals the end of data and should not be processed.

Output

follow the output format of sample output.

Sample Input

2 10 1 202 30 1 200 1 201 0 104 60 1 221 3 110 2 142 3 260 3 430 3 580 0

Sample Output

Instance #1: Not possibleInstance #2: 40Instance #3: 73

Source

Southeastern Europe 2006

———————————————————————————————————

题意:N个仓库由M条有向边连接,每条边都有一定费用。将两种危险品从0运到N-1,除了起点和终点外,危险品不能放在一起,也不能走相同的路径。求最小费用?

思路:除了起点和终点,不能放一起,说明普通顶点的容量都是1,起点终点容量为2。对所有边,追加容量=1;对起点,增加一个源点,建一条容量为2,费用为0的边,连到起点,对终点,增加一个汇点,从终点建一条容量为2,费用0的边到汇点。然后炮最小费用最大流

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;#define MAXN 60100#define MAXM 1000100int vis[MAXN],d[MAXN],pre[MAXN],a[MAXN];struct Edge{    int u, v, c, cost, next;} edge[MAXM];int s[MAXN], cnt;void init(){    cnt = 0;    memset(s, -1, sizeof(s));}void add(int u, int v, int c, int cost){    edge[cnt].u = u;    edge[cnt].v = v;    edge[cnt].cost = cost;    edge[cnt].c = c;    edge[cnt].next = s[u];    s[u] = cnt++;    edge[cnt].u = v;    edge[cnt].v = u;    edge[cnt].cost = -cost;    edge[cnt].c = 0;    edge[cnt].next = s[v];    s[v] = cnt++;}bool spfa(int ss, int ee,int &flow,int &cost){    queue<int> q;    memset(d, INF, sizeof d);    memset(vis, 0, sizeof vis);    d[ss] = 0, vis[ss] = 1, pre[ss] = 0, a[ss] = INF;    q.push(ss);    while (!q.empty())    {        int u = q.front();        q.pop();        vis[u] = 0;        for (int i = s[u]; ~i; i = edge[i].next)        {            int v = edge[i].v;            if (edge[i].c>0&& d[v]>d[u] + edge[i].cost)            {                d[v] = d[u] + edge[i].cost;                pre[v] = i;                a[v] = min(a[u], edge[i].c);                if (!vis[v])                {                    vis[v] = 1;                    q.push(v);                }            }        }    }    if (d[ee] == INF) return 0;    flow += a[ee];    cost += d[ee]*a[ee];    int u = ee;    while (u != ss)    {        edge[pre[u]].c -= a[ee];        edge[pre[u] ^ 1].c += a[ee];        u = edge[pre[u]].u;    }    return 1;}int MCMF(int ss, int ee){    int cost = 0, flow=0;    while (spfa(ss, ee, flow, cost));    if(flow<2)        return -1;    return cost;}int main(){    int m,n,u,v,c;    int cas=1;    while(~scanf("%d%d",&n,&m)&&(m||n))    {        init();        for(int i=0; i<m; i++)        {            scanf("%d%d%d",&u,&v,&c);            add(u,v,1,c);        }        add(n,0,2,0);        add(n-1,n+1,2,0);        printf("Instance #%d: ",cas++);        int ans=MCMF(n,n+1);        if(ans!=-1)            printf("%d\n",ans);        else            printf("Not possible\n");    }    return 0;}


原创粉丝点击