最小费用最大流uva10806+spfa

Problem ?
Dijkstra, Dijkstra.
Time Limit: 10 seconds
Dexter: "You don't understand. I can't walk...
they've tied my shoelaces together."

Topper Harley: "A knot. Bastards!"
Jim Abrahams and Pat Proft,
"Hot Shots! Part Deux."

You are a political prisoner in jail. Things are looking grim, but fortunately, your jailmate has come up with an escape plan. He has found a way for both of you to get out of the cell and run through the city to the train station, where you will leave the country. Your friend will escape first and run along the streets of the city to the train station. He will then call you from there on your cellphone (which somebody smuggled in to you inside a cake), and you will start to run to the same train station. When you meet your friend there, you will both board a train and be on your way to freedom.

Your friend will be running along the streets during the day, wearing his jail clothes, so people will notice. This is why you can not follow any of the same streets that your friend follows - the authorities may be waiting for you there. You have to pick a completely different path (although you may run across the same intersections as your friend).

What is the earliest time at which you and your friend can board a train?

Problem, in short
Given a weighed, undirected graph, find the shortest path from S to T and back without using the same edge twice.

Input
The input will contain several test cases. Each test case will begin with an integer n (2<=n<=100) - the number of nodes (intersections). The jail is at node number 1, and the train station is at node number n. The next line will contain an integer m - the number of streets. The next m lines will describe the mstreets. Each line will contain 3 integers - the two nodes connected by the street and the time it takes to run the length of the street (in seconds). No street will be longer than 1000 or shorter than 1. Each street will connect two different nodes. No pair of nodes will be directly connected by more than one street. The last test case will be followed by a line containing zero.

Output
For each test case, output a single integer on a line by itself - the number of seconds you and your friend need between the time he leaves the jail cell and the time both of you board the train. (Assume that you do not need to wait for the train - they leave every second.) If there is no solution, print "Back to jail".

Sample InputSample Output
2
1
1 2 999
3
3
1 3 10
2 1 20
3 2 50
9
12
1 2 10
1 3 10
1 4 10
2 5 10
3 5 10
4 5 10
5 7 10
6 7 10
7 8 10
6 9 10
7 9 10
8 9 10
0
Back to jail
80
Back to jail

思路:求两次最短路不能保证解最优,用最小费用最大流求,每条边的容量是1,原点s到1容量为2,n到汇点t容量为2,如果最大流小于2,无解,否则输出最小费用。

还要注意的是,图是无向图,加边的时候要加两条。

下面是代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxm=50010;
const int maxn=110;
const int INF=1000000000;
struct node
{
    int u,v,f,c,next;
} edge[maxm];
int n,m,num,head[maxn],pre[maxn],dis[maxn],vis[maxn];
void init()
{
    num=0;
    memset(head,-1,sizeof(head));
    memset(pre,-1,sizeof(pre));
}
void add_edge(int x,int y,int f,int c)
{
    edge[num].u=x;
    edge[num].v=y;
    edge[num].next=head[x];
    edge[num].f=f;
    edge[num].c=c;
    head[x]=num++;
    edge[num].u=y;
    edge[num].v=x;
    edge[num].next=head[y];
    edge[num].f=-f;
    edge[num].c=0;
    head[y]=num++;
}
int spfa(int s,int t)
{
    int ans=0;


    for(int i=s; i<=t; i++)
    {
        //pre[i]=-1;
        dis[i]=INF;
        vis[i]=0;
    }
    vis[s]=1;
    dis[s]=0;
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        int tmp=q.front();
        q.pop();
        vis[tmp]=0;
        for(int i=head[tmp]; i!=-1; i=edge[i].next)
        {
            if(edge[i].c>0)
            {
                int v=edge[i].v,w=edge[i].f;
                if(dis[v]>dis[tmp]+w)
                {
                    dis[v]=dis[tmp]+w;
                    pre[v]=i;
                    if(!vis[v])
                    {
                        vis[v]=1;
                        q.push(v);
                    }
                }
            }
        }
    }
    return dis[t]!=INF;

}
int mincost(int s,int t)
{
    int flow=INF,ans=0,f=0;
    while(spfa(s,t))
    {

        for(int i=pre[t]; i!=-1; i=pre[edge[i].u])
            flow=min(flow,edge[i].c);
        f+=flow;
        ans+=dis[t]*flow;
        for(int i=pre[t]; i!=-1; i=pre[edge[i].u])
        {
            edge[i].c-=flow;
            edge[i^1].c+=flow;
        }
    }
    if(f<2)return -1;
    else return ans;
}
int main()
{
    freopen("in.txt","r",stdin);
    while(scanf("%d",&n)!=EOF,n)
    {
        int s=0,t=n+1,x,y,f;
        scanf("%d",&m);
        init();
        add_edge(s,1,0,2);
        add_edge(n,t,0,2);
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&x,&y,&f);
            add_edge(x,y,f,1);
            //add_edge(y,x,f,1);
        }
        int ans=mincost(s,t);
        if(ans==-1)cout<<"Back to jail"<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值