1269 匈牙利游戏

2012年CCC加拿大高中生信息学奥赛

时间限制: 1 s
空间限制: 128000 KB
题目等级 : 钻石 Diamond
 
 
 
 
题目描述 Description

Welcome to the Hungary Games! The streets of Budapest form a twisted network of one-way streets.

欢迎来到匈牙利游戏!布达佩斯(匈牙利首都)的街道形成了一个弯曲的单向网络。

You have been forced to join a race as part of a “Reality TV” show where you race through these streets, starting at the Sz´echenyi thermal bath (s for short) and ending at the Tomb of G¨ ul Baba (t for short).

你被强制要求参加一个赛跑作为一个TV秀的一部分节目,比赛中你需要穿越这些街道,从s开始,到t结束。

Naturally, you want to complete the race as quickly as possible, because you will get more promo- tional contracts the better you perform.

很自然的,你想要尽快的完成比赛,因为你的比赛完成的越好,你就能得到更多的商业促销合同。

However, there is a catch: any person who is smart enough to take a shortest s-t route will be thrown into the P´alv¨olgyi cave system and kept as a national treasure. You would like to avoid this fate, but still be as fast as possible. Write a program that computes a strictly-second-shortest s-t route.

但是,有一个需要了解的是,如果有人过于聪明找到从s到t的最短路线,那么他就被扔到国家极品人类保护系统中作为一个国家宝藏收藏起来。你显然要避免这种事情的发生,但是也想越快越好。写一个程序来计算一个从s到t的严格次短路线吧。

Sometimes the strictly-second-shortest route visits some nodes more than once; see Sample Input 2 for an example.

有的时候,严格次短路线可能访问某些节点不止一次。样例2是一个例子。

输入描述 Input Description

The first line will have the format N M, where N is the number of nodes in Budapest and M is the number of edges. The nodes are 1,2,...,N; node 1 represents s; node N represents t. Then there are M lines of the form A B L, indicating a one-way street from A to B of length L. You can assume that A != B on these lines, and that the ordered pairs (A,B) are distinct.

第一行包含两个整数N和M,N代表布达佩斯的节点个数,M代表边的个数。节点编号从1到N。1代表出发点s,N代表终点t。接下来的M行每行三个整数A B L,代表有一条从A到B的长度为L的单向同路。你可以认为A不等于B,也不会有重复的(A,B)对。

输出描述 Output Description

Output the length of a strictly-second-shortest route from s to t. If there are less than two possible lengths for routes from s to t, output −1.

输出从s到t的严格次短路的长度。如果从s到t的路少于2条,输出-1。

样例输入 Sample Input

样例输入1:

4 6

1 2 5

1 3 5

2 3 1

2 4 5

3 4 5

1 4 13

样例输入2:

2 2

1 2 1

2 1 1

样例输出 Sample Output

样例输出1:

11

样例输出2:

3

数据范围及提示 Data Size & Hint

对于样例1:

There are two shortest routes of length 10 (1 → 2 → 4,1 → 3 → 4) and the strictly-second- shortest route is 1 → 2 → 3 → 4 with length 11.

对于样例2:

The shortest route is 1 → 2 of length 1, and the strictly-second route is 1 → 2 → 1 → 2 of length 3.

分类标签 Tags 点此展开

求次短路的问题

注意spfa中的三条注释

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=;
const int maxn=0xfffffff;
struct node
{
int u;
int v;
int w;
int next;
}edge[MAXN];
int num=;
int head[MAXN];
int n,m;
int dis[MAXN];
int vis[MAXN];// 是否在队列中
int tot=;
int pre[MAXN];// 记录次短路
void spfa()
{
dis[]=;
vis[]=;
queue<int>q;
q.push();
while(q.size()!=)
{
int p=q.front();
q.pop();
vis[p]=;
for(int i=head[p];i!=-;i=edge[i].next)
{
int to=edge[i].v;
if(dis[to]>dis[p]+edge[i].w)
{
pre[to]=dis[to];// 记录下更新之前的长度 即次长
dis[to]=dis[p]+edge[i].w;
//if(edge[i].v==n)tot++;
if(vis[to]==)
{
q.push(to);
vis[to]=;
}
}
else if(dis[to]!=dis[p]+edge[i].w&&pre[to]>dis[p]+edge[i].w)
{
pre[to]=dis[p]+edge[i].w;// 根据条件,此时需要更新,否则就不是次短路
if(vis[to]==)
{
q.push(to);
vis[to]=;
}
}
if(pre[to]>pre[p]+edge[i].w)// 同理,需要更新
{
pre[to]=pre[p]+edge[i].w;
if(vis[to]==)
{
q.push(to);
vis[to]=;
}
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
head[i]=-;
dis[i]=maxn;
pre[i]=maxn;
}
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&edge[num].u,&edge[num].v,&edge[num].w);
edge[num].next=head[edge[num].u];
head[edge[num].u]=num++;
}
spfa();
if(pre[n]!=maxn)
printf("%d",pre[n]);
else
{
printf("-1");
}
return ;
}

1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛的更多相关文章

  1. CODEVS——T 1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛

    http://codevs.cn/problem/1269/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Descript ...

  2. code vs1262 不要把球传我(组合数学) 2012年CCC加拿大高中生信息学奥赛

    1262 不要把球传我 2012年CCC加拿大高中生信息学奥赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题解  查看运行结果     题目描述 De ...

  3. 1267 老鼠的旅行 2012年CCC加拿大高中生信息学奥赛

    1267 老鼠的旅行  2012年CCC加拿大高中生信息学奥赛 题目描述 Description You are a mouse that lives in a cage in a large lab ...

  4. codevs 1262 不要把球传我 2012年CCC加拿大高中生信息学奥赛

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题目描述 Description CCC的足球比赛和传统的足球比赛有一点不同, 一次进球当且仅当先后接触到球的 ...

  5. 【动态规划】【记忆化搜索】【搜索】CODEVS 1262 不要把球传我 2012年CCC加拿大高中生信息学奥赛

    可以暴力递归求解,应该不会TLE,但是我们考虑记忆化优化. 设f(i,j)表示第i个数为j时的方案数. f(i,j)=f(1,j-1)+f(2,j-1)+……+f(i-1,j-1) (4>=j& ...

  6. codevs 1277 生活大爆炸 2012年CCC加拿大高中生信息学奥赛

     时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题目描述 Description Sheldon and Leonard are physicists wh ...

  7. codevs 1267 老鼠的旅行 2012年CCC加拿大高中生信息学奥赛

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description You are a mouse that lives in a cage in ...

  8. 求次短路 codevs 1269 匈牙利游戏

    codevs 1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Descriptio ...

  9. codevs——1269 匈牙利游戏

    1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Descript ...

随机推荐

  1. [codeforces821E]Okabe and El Psy Kongroo

    题意:(0,0)走到(k,0),每一部分有一条线段作为上界,求方案数. 解题关键:dp+矩阵快速幂,盗个图,注意ll 关于那条语句为什么不加也可以,因为我的矩阵C,就是因为多传了了len的原因,其他位 ...

  2. Flask07 Jinja2模板测试器、控制语句IF/FOR、变量/块 赋值、作用域、块级作用域

    1 测试器及其使用 在模板中的 {{}} 可以书写测试器,格式如下 {{ 变量 is 测试器名称  }} 1.1 在python中导入 Jinja2 的模板 from jinja2 import te ...

  3. try catch 块中debug时发现错误细节的一次记录

    在解决已有代码的一个问题时,有一个try catch块,基本代码如下: try { //do something } catch { LogHelper.Debug(typeof(myHelper), ...

  4. es6基础系列四--字符串的拓展

    1 for...of 字符串的遍历接口 for(let i of "abc"){ console.log(i); } // a // b // c 2 includes 是否包含某 ...

  5. streaming kafka direct 详解

    http://blog.cloudera.com/blog/2015/03/exactly-once-spark-streaming-from-apache-kafka/ http://www.jia ...

  6. SQL语法:MySQL系列之四

    一.SQL语言的简介和规范 ​ 是一种特定目的程序语言,用于管理关系数据库管理系统(RDBMS),或在关系流数据管理系统(RDSMS)中进行流处理. 20世纪70年代,IBM开发出SQL,用于DB2 ...

  7. Educational Codeforces Round 48 (Rated for Div. 2) D 1016D Vasya And The Matrix (构造)

    D. Vasya And The Matrix time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  8. jmeter读取本地CSV文件

    用jmeter录制考试上传成绩等脚本时,出现的问题及解决方法如下: 1.beanshell前置处理器,不能读取本地csv文件里的数据: 方法一: 在beanshell里不能直接从本地的csv文件里读取 ...

  9. 根据从redis缓存的数据查询出来,在从数据库中取出所有的数据,俩个数据进行比较,去掉重复,剩下库中新插入的数据,取出新数据,然后把redis中的缓存数据清空把从数据库中查出来的所有数据放到redis缓存中

    参考代码: public String getNewCenter(HttpServletRequest request,HttpServletResponse resonse){ JSONObject ...

  10. python3——“->”的含义

    ->:标记返回函数注释,信息作为.__annotations__属性提供,__annotations__属性是字典.键return是用于在箭头后检索值的键.但是在Python中3.5,PEP 4 ...