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);
    }
}



目录
相关文章
|
2天前
|
存储 JavaScript 前端开发
JavaScript基础
本节讲解JavaScript基础核心知识:涵盖值类型与引用类型区别、typeof检测类型及局限性、===与==差异及应用场景、内置函数与对象、原型链五规则、属性查找机制、instanceof原理,以及this指向和箭头函数中this的绑定时机。重点突出类型判断、原型继承与this机制,助力深入理解JS面向对象机制。(238字)
|
3天前
|
安全 数据可视化 网络安全
安全无小事|阿里云先知众测,为企业筑牢防线
专为企业打造的漏洞信息收集平台
1303 2
|
4天前
|
云安全 人工智能
2025,阿里云安全的“年度报告”
拥抱AI时代,阿里云安全为你护航~
1453 2
|
2天前
|
人工智能 自然语言处理 API
n8n:流程自动化、智能化利器
流程自动化助你在重复的业务流程中节省时间,可通过自然语言直接创建工作流啦。
357 4
n8n:流程自动化、智能化利器
|
11天前
|
机器学习/深度学习 安全 API
MAI-UI 开源:通用 GUI 智能体基座登顶 SOTA!
MAI-UI是通义实验室推出的全尺寸GUI智能体基座模型,原生集成用户交互、MCP工具调用与端云协同能力。支持跨App操作、模糊语义理解与主动提问澄清,通过大规模在线强化学习实现复杂任务自动化,在出行、办公等高频场景中表现卓越,已登顶ScreenSpot-Pro、MobileWorld等多项SOTA评测。
1467 7
|
1天前
|
Linux 数据库
Linux 环境 Polardb-X 数据库 单机版 rpm 包 安装教程
本文介绍在CentOS 7.9环境下安装PolarDB-X单机版数据库的完整流程,涵盖系统环境准备、本地Yum源配置、RPM包安装、用户与目录初始化、依赖库解决、数据库启动及客户端连接等步骤,助您快速部署运行PolarDB-X。
224 1
Linux 环境 Polardb-X 数据库 单机版 rpm 包 安装教程
|
4天前
|
人工智能 API 开发工具
Skills比MCP更重要?更省钱的多!Python大佬这观点老金测了一周终于懂了
加我进AI学习群,公众号右下角“联系方式”。文末有老金开源知识库·全免费。本文详解Claude Skills为何比MCP更轻量高效:极简配置、按需加载、省90% token,适合多数场景。MCP仍适用于复杂集成,但日常任务首选Skills。推荐先用SKILL.md解决,再考虑协议。附实测对比与配置建议,助你提升效率,节省精力。关注老金,一起玩转AI工具。
|
12天前
|
人工智能 Rust 运维
这个神器让你白嫖ClaudeOpus 4.5,Gemini 3!还能接Claude Code等任意平台
加我进AI讨论学习群,公众号右下角“联系方式”文末有老金的 开源知识库地址·全免费
1335 16
|
4天前
|
机器学习/深度学习 测试技术 数据中心
九坤量化开源IQuest-Coder-V1,代码大模型进入“流式”训练时代
2026年首日,九坤创始团队成立的至知创新研究院开源IQuest-Coder-V1系列代码大模型,涵盖7B至40B参数,支持128K上下文与GQA架构,提供Base、Instruct、Thinking及Loop版本。采用创新Code-Flow训练范式,模拟代码演化全过程,提升复杂任务推理能力,在SWE-Bench、LiveCodeBench等基准领先。全阶段checkpoint开放,支持本地部署与微调,助力研究与应用落地。
440 1
|
3天前
|
安全 API 开发者
手把手带你使用无影 AgentBay + AgentScope 完成一站式智能体开发部署
阿里云无影 AgentBay 作为一个面向 AI 智能体开发的云端 GUI 沙箱服务,已集成至阿里巴巴通义实验室开源的 AgentScope 框架,助力开发者快速构建安全、高效的智能体应用。
259 1

热门文章

最新文章