uva 10806 Dijkstra, Dijkstra.

原题:
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 m streets. 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 Input
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
Sample Output
Back to jail
80
Back to jail

中文:
给你一个带权无向图,让你找从节点1到节点n,然后再从节点n走回节点1所用的费用最小。

#include<bits/stdc++.h>
using namespace std;
const int maxn=10001;
int mark;
struct Edge
{
    int from,to,cap,flow,cost;
    Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w){}
};

struct MCMF
{
    int n,m;
    vector<Edge> edges;
    vector<int> G[maxn];
    int inq[maxn];
    int d[maxn];
    int p[maxn];
    int a[maxn];

    void init(int n)
    {
        this->n=n;
        for(int i=1;i<=n;i++)
            G[i].clear();
        edges.clear();
    }

    void AddEdge(int from,int to,int cap,int cost)
    {
        edges.push_back(Edge(from,to,cap,0,cost));
        edges.push_back(Edge(to,from,0,0,-cost));
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool BellmanFord(int s,int t,int& flow,long long& cost)
    {
        for(int i=1;i<=n;i++)
            d[i]=INT_MAX;
        memset(inq,0,sizeof(inq));
        d[s]=0;
        inq[s]=1;
        p[s]=0;
        a[s]=INT_MAX;

        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=edges[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]==INT_MAX)
            return false;
        flow+=a[t];
        cost+=(long long )d[t]*(long long)a[t];
        for(int u=t;u!=s;u=edges[p[u]].from)
        {
            edges[p[u]].flow+=a[t];
            edges[p[u]^1].flow-=a[t];
        }
        return true;
    }

    int MincostMaxflow(int s,int t,long long& cost)
    {
        int flow = 0;
        cost=0;
        int road=0;
        while(BellmanFord(s,t,flow,cost))
        {
            road++;
            if(road==2)
                break;
        }
        if(road==2)
            mark=true;
        return flow;
    }
};
MCMF M;
int main()
{
    ios::sync_with_stdio(false);
    int n,m;
    while(cin>>n,n)
    {
        M.init(n);
        mark=false;
        cin>>m;
        for(int i=1;i<=m;i++)
        {
            int f,t,c;
            cin>>f>>t>>c;
            M.AddEdge(f,t,1,c);
            M.AddEdge(t,f,1,c);
        }
        long long cost=0;
        M.MincostMaxflow(1,n,cost);
        if(!mark)
            cout<<"Back to jail"<<endl;
        else
            cout<<cost<<endl;
    }
    return 0;
}

解答:
这是我第一道最小费用最大流的题目,刚开始被这道题的名字给忽悠了,一直以为是最短路问题,但是想了半天也想不出怎么解(貌似可以用次短路径来解决)。上网上一搜,结果一看是网络流的题目-_-。

这里把图当中的所有边上的流量都设置为1,由于是无向图,所以需要在两个节点之间建立两条相同的边。
题目中要求从起点走到终点,再从终点回到起点,这里要用到网络流处理当中的特性,如果走过某一条边,那么这条变上要增加对应的流量,同时反向边要减少对应的流量。这也是网络流当中的精华和核心部分,也是网络流算法为什么能解决这个问题的根本所在。
现在起点是s终点是t,如果第一条路径走的是s->a->b->c->t,那么边上的流量就会发生改变,第二次搜索到的路径可能是s->b->a->d->t,那么两条路径权值和起来就是s->c->t加上s->d->t的值的和。所以,使用最小费用最大流算法可以解决此问题,相当于从起点找两条到终点的最小费用的增广路径,两次的费用和就是答案。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值