uva 10806 Dijkstra, Dijkstra. dj和网络

题目:

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 rst 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, nd 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

 

题目大意:

给个n表示n个点,然后给出m个线,表示xy两点费用为t个时间到达(双向)

求分别走两次,不能走同一个边上。

题目思路:

因为两次不能走在一条边上且要求最小费用,所以不能用两次dj搜索,所以可以用网络流的思路去进行第二次搜索。

所以出现两种方法,

1第二次用fl搜索可能会超时

2第二次用dj的剪纸形式

程序:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cctype>
#include <fstream>
#include <limits>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cassert>
using namespace std;
const int inf=1<<20;
const int maxn=10000;
struct node//每个点
{
    int x,f,fat;
    friend bool operator < (node a,node b)
    {
        return a.f>b.f;
    }
}a,b;
int n,m;
bool v[maxn+10];
int t[maxn+10][maxn+10];
int zhi[maxn+10],father[maxn+10];
int in();
int out(int,int);
int run(int);
int pop();
int main()
{
    while(in())
    {
        int ans=run(0),ans2;
        //cout<<'@'<<ans;
        pop();
        if(ans&&ans!=inf)
        ans2=run(1);
        out(ans,ans2);
    }
return 0;
}
int in()
{
    scanf("%d",&n);
    if(!n)return 0;
      scanf("%d",&m);
      memset(t,0,sizeof(t));
      memset(father,0,sizeof(father));
      for(int i=0;i<m;i++)
      {
          int st,ed,fei;
          scanf("%d%d%d",&st,&ed,&fei);
          t[st][ed]=fei;
          t[ed][st]=fei;
      }
    return 1;
}
int run(int first)
{
    memset(v,0,sizeof(v));
    for(int i=1;i<=n;i++)
        zhi[i]=inf;
    priority_queue<node>pqu;
    a.x=1,a.f=0,zhi[1]=0,a.fat=1;
    pqu.push(a);
    while(!pqu.empty())
    {
        a=pqu.top();//cout<<'!'<<a.x<<a.f<<endl;
        pqu.pop();
        v[a.x]=1;
        if(zhi[a.x]>a.f||!zhi[a.x])
        {
            zhi[a.x]=a.f;
            father[a.x]=a.fat;
        }
        if(a.x==n)
            return zhi[a.x];
        for(int i=2;i<=n;i++)
            if(t[a.x][i])
                if(first||!v[i])
        {
            b.x=i;
            b.f=zhi[a.x]+t[a.x][i];
            b.fat=a.x;
            if(zhi[i]>b.f)
            pqu.push(b);
        }
    }
    return 0;
}
int pop()
{
    int i=n;
    while(i>1)
    {
        t[father[i]][i]=0;
        t[i][father[i]]*=-1;
        i=father[i];
    }
}
int out(int ans,int ans2)
{
    if(ans&&ans2&&ans2!=inf)
        printf("%d\n",ans+ans2);
    else
        printf("Back to jail\n");
}


你可能感兴趣的:(ACM)