UVA10806 用SPFA

简介: Problem ?Dijkstra, Dijkstra.Time Limit: 10 seconds   Dexter: "You don't understand. I can't walk.
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 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

题意:有A,B两个人要越狱,A成功地从监狱到达火车站时B立即出发,两个人的路线不能有重合(可以重合点,不可以重合边),需要两个人路径和最短,求最短路径和。
抽象一点,就是找到从点S到T的最短长度的环(即:两次路径不能有重边)

思路:最大流的方法可以做,我用的是两次SPFA,相当于求两次最短路:先求一次最短路,然后把最短路上的S->T方向的边长赋值为INF,反向边长赋值为-map[u][v],即原来长度的相反数,然后再次SPFA,两次结果相加即可。
对于标记为反向长度取反,如果不懂,请看下图:


输入数据为:
4
5
1 2 1
2 3 1
3 4 1
1 3 10
2 4 10

上图中红色路线为第一条最短路,绿色路线为第二条最短路。两条最短路有公共边,这时候每条路线可以看成:前一部分+公共边+后一部分,在公共边处由于做了反向长度的标记,那么公共部分相当于没有走(长度相互抵消了),实际效果等价于“红色路线的前一部分+绿色路线的后一部分”作为第一条路线,“绿色路线前一部分+红色路线后一部分”作为第二条路线。

#include <stdio.h>
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 100 + 5;
int map[MAXN][MAXN];
int vis[MAXN];
int d[MAXN];
int fa[MAXN];
void init(int n){
    int i, j;
    for(i=1; i<=n; i++){
        for(j=1; j<=n; j++){
            if(i!=j) map[i][j] = INF;
            else map[i][j] = 0;
        }
    }
}
int SPFA(int S, int T, int n){
    int i;
    memset(vis, 0, sizeof(vis));
    memset(d, 63, sizeof(d));
    d[S] = 0;
    queueq;
    q.push(S);
    vis[S] = 1;
    while(!q.empty()){
        int x = q.front();
        q.pop();
        vis[x] = 0;
        for(i=1; i<=n; i++){
            if(d[i]>d[x]+map[x][i]){
                d[i] = d[x] + map[x][i];
                fa[i] = x;
                if(!vis[i]){
                    vis[i] = 1;
                    q.push(i);
                }
            }
        }
    }
    return d[T];
}
int main(){
//    freopen("in.txt", "r", stdin);
    int n, m;
    while(scanf("%d", &n)!=EOF && n){
        scanf("%d", &m);
        init(n);
        int i, u, v, w;
        for(i=1; i<=m; i++){
            scanf("%d%d%d", &u, &v, &w);
            if(map[u][v]>w){
                map[u][v] = map[v][u] = w;
            }
        }
        int S = 1, T = n;
        int ans1 = SPFA(S, T, n);
        while(T!=S){
            map[T][fa[T]] = -map[T][fa[T]];
            map[fa[T]][T] = INF;
            T = fa[T];
        }
        T = n;
        int ans2 = SPFA(S, T, n);
        if(ans1 >= INF || ans2 >= INF) printf("Back to jail\n");
        else printf("%d\n", ans1 + ans2);
    }
}



目录
相关文章
|
存储 缓存 固态存储
408计算机组成原理学习笔记——存储系统(一)
408计算机组成原理学习笔记——存储系统
2068 2
408计算机组成原理学习笔记——存储系统(一)
|
网络安全
jmeter录制HTTPS的脚本
jmeter录制HTTPS的脚本
jmeter录制HTTPS的脚本
|
4月前
|
机器学习/深度学习 数据采集 人工智能
9类番茄病害识别数据集(5000张)|YOLO训练数据集 农业AI 病害识别 智慧农业 作物监测
本数据集含5000张高质量番茄叶片图像,覆盖早疫病、晚疫病、叶霉病、花叶病毒等8类常见病害及健康叶片,共9类。采用YOLO标准标注,结构规范(train/valid/test),适配YOLOv5/v8等模型,助力农业AI病害识别与智慧监测。
|
前端开发 JavaScript
canvas系列教程01——直线、三角形、多边形、矩形、调色板
canvas系列教程01——直线、三角形、多边形、矩形、调色板
618 0
|
缓存 Ubuntu Linux
Linux buffer/cache内存占用过高
Linux内核会在内存将要耗尽的时候,触发内存回收的工作,以便释放出内存给急需内存的进程使用。一般情况下,这个操作中主要的内存释放都来自于对buffer/cache的释放。尤其是被使用更多的cache空间。
4901 0
Linux buffer/cache内存占用过高
|
存储 关系型数据库 MySQL
MySQL的MyISAM引擎:技术特点与应用场景
【4月更文挑战第20天】MySQL的MyISAM引擎特点是表级锁定,适合读多写少的场景,不支持事务但提供全文索引,适用于只读应用、全文搜索和简单备份恢复。在选择存储引擎时,应根据具体需求权衡。
1574 11
|
负载均衡 算法 网络虚拟化
ensp中链路聚合配置命令
链路聚合(Link Aggregation)是结合多条物理链路形成逻辑链路的技术,提升网络带宽、增强冗余性和优化负载均衡。在高带宽、高可靠性及负载均衡需求的场景如服务器集群、数据中心等中广泛应用。配置包括手动和自动模式,手动模式下,如LSW1和LSW2,通过`int eth-trunk`、`trunkport`等命令配置接口和成员链路。自动模式下,如SW3和LSW4,使用LACP协议动态聚合,通过`mode lacp-static`和`load-balance dst-mac`命令设置。配置后,使用`dis eth-trunk`检查聚合状态。
3321 1
ensp中链路聚合配置命令
|
存储 缓存
什么是云存储设备
什么是云存储设备
1256 0
|
小程序
单店营收平均提升达8%-10% 阿里系小程序矩阵的风口来了!
支付宝小程序与天猫打通后的首款产品——品牌轻店正式亮相,无需商家单独开发,只要在天猫新零售工作台简单配置就能自动生成支付宝端的品牌轻店小程序。
1340 14
单店营收平均提升达8%-10% 阿里系小程序矩阵的风口来了!
分析研究&lt;&lt;一战到底&gt;&gt;节目规则演变
分析研究节目规则演变 一、研究范围 江苏卫视2012年3月2日推出益智答题类节目 研究时间截止:2014年1月4日星期六 二、规则演变 1.初始规则 2012年3月2日规则 1.每期参加节目的有11人,分为10位守擂者和1位挑战者,守擂者手中都有不同价值的奖品,而挑战者将通过20秒的限时答题与守擂者一一PK,挑战者获胜将赢得守擂者的奖品,而守擂者获胜将成为新的挑战者,并赢取挑战者手中的所有奖品,任何一方失败都将掉下擂台淘汰出局。
1094 0

热门文章

最新文章