codevs 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.

 /*直接利用SPFA维护一个点到另一个点的最短路和次短路,维护方法如下:
1、如果from的最短路能更新to的最短路,就让更新之前的最短路等于次短路,然后去更新最短路。
2、如果from的最短路不能跟新to的最短路,但是可以更新次短路,就去更新次短路。
3、如果form的最短路不能跟新to的最短路,也不能更新次短路,但是from的次短路可以更新to的次短路,那么就去更新次短路。 */
#include<algorithm>
#include<queue>
#include<iostream>
using namespace std;
#include<cstdio>
#define N 50010
#define inf (1<<30)-1
bool inque[N]={};
int n,m,a,b,l;
long long dis[N],cdis[N];
int head[N];
struct Edge{
int v,w,last;
}edge[];
int t=;
void add_edge(int u,int v,int w)
{
++t;
edge[t].v=v;
edge[t].w=w;
edge[t].last=head[u];
head[u]=t;
}
void input()
{
cin>>n>>m;
//scanf("%d%d",&n,&m);
for(int i=;i<=m;++i)
{
cin>>a>>b>>l;
//scanf("%d%d%d",&a,&b,&l);
add_edge(a,b,l);
}
for(int i=;i<=n;++i)
dis[i]=cdis[i]=inf;
}
void spfa(int k)
{
queue<int>Q;
Q.push(k);
inque[k]=true;
dis[k]=;//
while(!Q.empty())
{
int x=Q.front();
Q.pop();
inque[x]=false;
for(int l=head[x];l;l=edge[l].last)
{
if(dis[x]+edge[l].w<dis[edge[l].v])
{
cdis[edge[l].v]=dis[edge[l].v];
dis[edge[l].v]=dis[x]+edge[l].w;
if(!inque[edge[l].v])
{
inque[edge[l].v]=true;
Q.push(edge[l].v);
}
}
else if(edge[l].w+dis[x]>dis[edge[l].v]&&dis[x]+edge[l].w<cdis[edge[l].v])//
{
cdis[edge[l].v]=dis[x]+edge[l].w;
if(!inque[edge[l].v])
{
inque[edge[l].v]=true;
Q.push(edge[l].v);
}
}
else if(cdis[edge[l].v]>cdis[x]+edge[l].w)//
{
cdis[edge[l].v]=cdis[x]+edge[l].w;
if(!inque[edge[l].v])
{
inque[edge[l].v]=true;
Q.push(edge[l].v);
}
}
}
}
}
int main()
{
input();
spfa();
if(cdis[n]==inf) cout<<-;
else cout<<cdis[n]<<endl;
return ;
}

求次短路 codevs 1269 匈牙利游戏的更多相关文章

  1. codevs——1269 匈牙利游戏

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

  2. codevs 1269 匈牙利游戏——次短路(spfa)

    欢迎来到匈牙利游戏!布达佩斯(匈牙利首都)的街道形成了一个弯曲的单向网络. 你被强制要求参加一个赛跑作为一个TV秀的一部分节目,比赛中你需要穿越这些街道,从s开始,到t结束. 很自然的,你想要尽快的完 ...

  3. codevs 1269 匈牙利游戏

    /*暴力+乱搞 55分(似乎只有暴力得分了)*/ #include<iostream> #include<cstdio> #include<cstring> #in ...

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

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

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

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

  6. 【wikioi】1269 匈牙利游戏(次短路+spfa)

    http://www.wikioi.com/problem/1269/ 噗,想不到.. 次短路就是在松弛的时候做下手脚. 设d1为最短路,d2为次短路 有 d1[v]>d1[u]+w(u, v) ...

  7. C++之路进阶codevs1269(匈牙利游戏)

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

  8. 匈牙利游戏(codevs 1269)

    题目描述 Description Welcome to the Hungary Games! The streets of Budapest form a twisted network of one ...

  9. HDU 1688 Sightseeing&HDU 3191 How Many Paths Are There(Dijkstra变形求次短路条数)

    Sightseeing Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

随机推荐

  1. Python正则表达式模块(re模块)

    Python是我接触到的第一门编程语言,虽然它足够简单,但是对于当时刚刚接触编程语言的我来说还是有些难度的,于是只是了解了一些Python的基本语法,稍微深入一点的地方都没怎么了解.不过,到现在为止, ...

  2. SQL Server SQL语句执行顺序

    执行顺序: 1.FROM:对FROM子句中前两个表执行笛卡尔积生成虚拟表vt1 2.ON:对vt1表应用ON筛选器只有满足 为真的行才被插入vt2 3.OUTER(join):如果指定了 OUTER ...

  3. D/A转换器

    电荷:带正负电的基本粒子.电的本质是使正负电荷分开,使电荷发生移动,实质是电子的转移,并不是创造电荷.电压:单位正电荷受电场力作用从A点移动到B点所做的功.电压方向从高电位指向低点位.电压是推动电荷定 ...

  4. c#泛型方法返回null的问题

    c#的泛型方法实现和java实现有点不同,在java中,所有的泛型方法运行时类型必须是引用类型,所以和非泛型一样可以返回null. 但是c#中有点不同,可以同时是值类型和引用类型,而值类型不能赋值nu ...

  5. mysql 5.6.33发布

    2016-09-06,mysql 5.6.33社区版发布,修复的bug越发减少,而且基本上都是较少使用的特性.

  6. NYOJ 1058 部分和问题

    部分和问题 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 给定整数a1.a2........an,判断是否可以从中选出若干数,使它们的和恰好为K.   输入 首先, ...

  7. margin:0 auto;不能居中的原因

    原因: 1.没有设置本身元素和父元素的宽度 2.本身元素使用了绝对定位和浮动 2.没声明DOCTYPE

  8. 极光推送和百度lbs android sdk一起使用使用proguard 混淆的问题

    主要是http得类被混淆后,导致apk定位失败.经过确认,保留apache 的http类就好了 # To enable ProGuard in your project, edit project.p ...

  9. JAVA基础学习day20--IO流二-缓冲流、字节流

    一.缓冲流 1.1.字符流的缓冲区 缓冲区的出现是为了提高IO的读写效率 对应类 BufferedReader BufferedWriter 缓冲区要结合流才可以使用 在流的基础上对流的功能进行了增强 ...

  10. 我为什么用 SQLite 和 FMDB 而不用 Core Data

    凭良心讲,我不能告诉你不去使用Core Data.它不错,而且也在变好,并且它被很多其他Cocoa开发者所理解,当有新人加入你的组或者需要别人接手你的项目的时候,这点很重要.更重要的是,不值得花时间和 ...