UVa10806_Dijkstra, Dijkstra.(网络流/费用流)(小白书图论专题)

解题报告

思路:

从s->t 再从t->s等同与s->t两次,要求每条路只能走一次,要求最小花费,让每一条边容量为1,跑跑费用流

只要跑出流量为2就结束。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define inf 0x3f3f3f3f
#define N 5000
#define M 50000

using namespace std;
int n,m,s,t,cnt,head[N],pre[N],vis[N],dis[N],f[N],cost,flow;
struct node {
    int v,cost,cap,next;
} edge[M];
void add(int u,int v,int cost,int cap) {
    edge[cnt].v=v,edge[cnt].cost=cost,edge[cnt].cap=cap;
    edge[cnt].next=head[u],head[u]=cnt++;
    edge[cnt].v=u,edge[cnt].cost=-cost,edge[cnt].cap=0;
    edge[cnt].next=head[v],head[v]=cnt++;
}
int _spfa() {
    for(int i=1; i<=n; i++) {
        dis[i]=inf;
        vis[i]=pre[i]=f[i]=0;
    }
    dis[1]=0;
    vis[1]=1;
    f[1]=inf;
    pre[1]=-1;
    queue<int >Q;
    Q.push(1);
    while(!Q.empty()) {
        int u=Q.front();
        Q.pop();
        vis[u]=0;
        for(int i=head[u]; i!=-1; i=edge[i].next) {
            int v=edge[i].v;
            if(edge[i].cap&&dis[v]>dis[u]+edge[i].cost) {
                dis[v]=dis[u]+edge[i].cost;
                f[v]=min(f[u],edge[i].cap);
                pre[v]=i;
                if(!vis[v]) {
                    vis[v]=1;
                    Q.push(v);
                }
            }
        }
    }
    if(dis[n]==inf)return 0;
    cost+=f[n]*dis[n];
    flow+=f[n];
    if(flow==2)return 0;
    for(int i=pre[n]; i!=-1; i=pre[edge[i^1].v]) {
        edge[i].cap-=f[n];
        edge[i^1].cap+=f[n];
    }
    return 1;
}
void mcmf() {
    cost=flow=0;
    while(_spfa());
}
int main() {
    int i,j,u,v,w;
    while(~scanf("%d",&n)) {
        if(!n)break;
        memset(head,-1,sizeof(head));
        cnt=0;
        scanf("%d",&m);
        while(m--) {
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w,1);
            add(v,u,w,1);
        }
        mcmf();
        if(flow!=2||cost==inf)
            printf("Back to jail\n");
        else printf("%d\n",cost);
    }
    return 0;
}

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 integerm - 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 Sample 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

Problemsetter: Igor Naverniouk

时间: 2024-10-29 03:35:52

UVa10806_Dijkstra, Dijkstra.(网络流/费用流)(小白书图论专题)的相关文章

UVa563_Crimewave(网络流/最大流)(小白书图论专题)

解题报告 思路: 要求抢劫银行的伙伴(想了N多名词来形容,强盗,贼匪,小偷,sad.都认为不合适)不在同一个路口相碰面,能够把点拆成两个点,一个入点.一个出点. 再设计源点s连向银行位置.再矩阵外围套上一圈.连向汇点t 矩阵内的点,出点和周围的点的出点相连. #include <iostream> #include <cstring> #include <cstdio> #include <queue> #define M 500000 #define N

UVa10048_Audiophobia(最短路/floyd)(小白书图论专题)

解题报告 题意: 求所有路中最大分贝最小的路. 思路: 类似floyd算法的思想,u->v可以有另外一点k,通过u->k->v来走,拿u->k和k->v的最大值和u->v比较,存下最小的值. #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define inf 0x3f3f3f3f using namespace std;

UVa10397_Connect the Campus(最小生成树)(小白书图论专题)

解题报告 题目传送门 题意: 使得学校网络互通的最小花费,一些楼的线路已经有了. 思路: 存在的线路当然全都利用那样花费肯定最小,把存在的线路当成花费0,求最小生成树 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define inf 0x3f3f3f3f using namespace std; int n,m,_hash[1110][1110],vis

UVa10099_The Tourist Guide(最短路/floyd)(小白书图论专题)

解题报告 题意: 有一个旅游团现在去出游玩,现在有n个城市,m条路.由于每一条路上面规定了最多能够通过的人数,现在想问这个旅游团人数已知的情况下最少需要运送几趟 思路: 求出发点到终点所有路当中最小值最大的那一条路. 求发可能有多种,最短路的松弛方式改掉是一种,最小生成树的解法也是一种(ps,prime和dijs就是这样子类似的) #include <iostream> #include <cstdio> #include <cstring> #include <

UVa10034/POJ2560_Freckles(最小生成树)(小白书图论专题)

解题报告 题意: 把所有点连起来,求使用的墨水最少. 思路: 裸最小生成树. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #define inf 0x3f3f3f3f using namespace std; struct N { double x,y; } node[110]; int vis[110],n; double mmap[110][110],

UVa10801_Lift Hopping(最短路)(小白书图论专题)

解题报告 思路 神奇的电梯,我的思路是直接整出一个超级源点和超级汇点(貌似这是网络流的叫法,,,sad) 源点与所有有在0层的电梯连线,汇点与k层连线,然后每个电梯如果有在同一层的连60s的线,对于每个电梯可以到达的每一层连一条线,处理层和电梯就直接用类似于离散化的方式处理,比如说第一个电梯可以有n个层可以到,第二个电梯有m个层可以到,那么就有1-n+m的点,源点0,汇点n+m+1: 做完看来别人的解题报告,发现我的想法很辍,因为只有在换电梯的时候才要松弛加上60,用邻接矩阵处理就很好. 智商捉

UVa10803_Thunder Mountain(最短路)(小白书图论专题)

解题报告 裸floyd. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #define inf 0x3f3f3f3f using namespace std; int n; double mmap[210][210]; struct node { double x,y; } p[210]; double disc(node p1,node p2) { ret

UVa558_Wormholes(最短路)(小白书图论专题)

解题报告 思路: spfa判负环. #include <iostream> #include <cstring> #include <cstdio> #include <queue> #define inf 0x3f3f3f3f #define N 40000 #define M 100000 using namespace std; struct node { int v,w,next; } edge[M]; int head[N],dis[N],vis[

UVa10986_Sending email(最短路)(小白书图论专题)

解题报告 思路: 裸裸的最短路. #include <iostream> #include <cstring> #include <cstdio> #include <queue> #define inf 0x3f3f3f3f #define N 40000 #define M 100000 using namespace std; struct node { int v,w,next; }edge[M]; int head[N],dis[N],vis[N]