Poj 3068 "Shortest" pair of paths【拆点+最小费用最大流】

"Shortest" pair of paths
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1288 Accepted: 545

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 1
0 1 20
2 3
0 1 20
0 1 20
1 0 10
4 6
0 1 22
1 3 11
0 2 14
2 3 26
0 3 43
0 3 58
0 0

Sample Output

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

Source


题目大意:

求两次从0到n-1的最短路,要求每一条路都没有重叠的点(当然抛去起点和终点),如果可能,输出最短路之和,否则输出Not possible.


思路:

2、我们对点的限制加一个容量,那么就能解决一个点不能重复走两次的问题,而流量问题是建立在边之上的,并不是点,那么我们将一个点一分为二.设定一条有向边,然后设定其流量为1,表示这条边只能走一次,那么就相当于这个点只能走一次了。


3、大概思路构建完毕,剩下的就是去实现建图了:

①建立源点S,连入0号节点,其流设定为2,花费为0,表示跑两次最短路。

②建立汇点T,N-1号节点的拆点连入汇点T,其流设定为2,花费为0,表示跑两次最短路。

③每个点一分为二.两点之间建立一条有向边,其流设定为1,花费为0,表示这个点只能走一次,(当然0号节点和n-1号节点要设定为2)。

④然后将输入进来的边加入网络中,流设定为1,花费为对应边的权值。


4、跑一遍连续增广路算法。如果求出的最大流==2,那么表示任务能够成功,输出最小花费,否则输出任务失败。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node
{
    int from;
    int to;
    int num;
    int w;
    int f;
    int next;
}e[1500000];
int head[10000];
int dis[10000];
int pre[10000];
int vis[10000];
int path[10000];
int n,m,cont,ss,tt;
void add(int from,int to,int f,int w)
{
    /*
    if(f)
    {
        printf("%d %d %d\n",from,to,w);
    }*/
    e[cont].to=to;
    e[cont].w=w;
    e[cont].f=f;
    e[cont].num=cont;
    e[cont].next=head[from];
    head[from]=cont++;
}
void Getmap()
{
    cont=0;
    ss=n*2+1;
    tt=ss+1;
    memset(head,-1,sizeof(head));
    add(ss,1,2,0);
    add(1,ss,0,0);
    add(n+n,tt,2,0);
    add(tt,n+n,0,0);
    for(int i=1;i<=n;i++)
    {
        if(i==1||i==n)
        {
            add(i,i+n,2,0);
            add(i+n,i,0,0);
            continue;
        }
        add(i,i+n,1,0);
        add(i+n,i,0,0);
    }
    for(int i=0;i<m;i++)
    {
        int x,y,w;
        scanf("%d%d%d",&x,&y,&w);
        x++,y++;
        add(x+n,y,1,w);
        add(y,x+n,0,-w);
    }
}
int SPFA()
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=tt;i++)dis[i]=0x3f3f3f3f;
    dis[ss]=0;
    queue<int >s;
    s.push(ss);
    while(!s.empty())
    {
        int u=s.front();
        s.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            int w=e[i].w;
            int f=e[i].f;
            if(f&&dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                pre[v]=u;
                path[v]=e[i].num;
                if(vis[v]==0)
                {
                    vis[v]=1;
                    s.push(v);
                }
            }
        }
    }
    if(dis[tt]!=0x3f3f3f3f)return 1;
    else return 0;
}
void MCMF()
{
    int ans=0;
    int maxflow=0;
    while(SPFA()==1)
    {
        int minn=0x3f3f3f3f;
        for(int i=tt;i!=ss;i=pre[i])
        {
            minn=min(minn,e[path[i]].f);
        }
        for(int i=tt;i!=ss;i=pre[i])
        {
            e[path[i]].f-=minn;
            e[path[i]^1].f+=minn;
        }
        maxflow+=minn;
        ans+=dis[tt]*minn;
    }
    if(maxflow==2)
    {
        printf("%d\n",ans);
    }
    else printf("Not possible\n");
}
int main()
{
    int kase=0;
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)break;
        Getmap();
        printf("Instance #%d: ",++kase);
        MCMF();
    }
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值