题目描述 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
n<=20000,m<=100000

对于样例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一遍,再把边反向spfa一遍,求出dis1和dis2
枚举每条边,设这条边一定要走,求出这种情况下的最短路,最后在所有情况中找出严格次短路
对于这个方法的理解:
因为如果求不出严格次短路,说明严格次短路中的某条路没有走过,那么我们可以让这条路一定走,
那么经过这条路的最短路一定是严格次短路,所以要把所有的点枚举一遍
注意:这个方法求不出次短路,因为如果这样做,最短路可能走过两遍,但我们是分辨不出来的,
这对本题没有影响,因为严格次短路的长度与最短路不同,但次短路可能与最短路长度相同,
所以不对。
*/
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#define N 20010
#define M 100010
#define INF 1061109567
using namespace std;
struct node
{
int v,t,pre;
};node e1[M],e2[M];
int dis1[N],dis2[N],head1[N],head2[N],vis[N],f[M],n,m;
queue<int> q1;
queue<int> q2;
int read()
{
char c=getchar();int num=,flag=;
while(c<''||c>''){if(c=='-')flag=-;c=getchar();}
while(c>=''&&c<=''){num=num*+c-'';c=getchar();}
return num*flag;
}
void add1(int cnt,int x,int y,int z)
{
e1[cnt].v=y;
e1[cnt].t=z;
e1[cnt].pre=head1[x];
head1[x]=cnt;
}
void add2(int cnt,int x,int y,int z)
{
e2[cnt].v=y;
e2[cnt].t=z;
e2[cnt].pre=head2[x];
head2[x]=cnt;
}
void spfa1()
{
memset(dis1,0x3f3f3f3f,sizeof(dis1));
q1.push();
vis[]=;
dis1[]=;
while(!q1.empty())
{
int x=q1.front();
q1.pop();
for(int i=head1[x];i;i=e1[i].pre)
{
int y=e1[i].v;
if(dis1[y]>dis1[x]+e1[i].t)
{
dis1[y]=dis1[x]+e1[i].t;
if(!vis[y])
{
q1.push(y);
vis[y]=;
}
}
}
}
}
void spfa2()
{
memset(vis,,sizeof(vis));
memset(dis2,0x3f3f3f3f,sizeof(dis2));
q2.push(n);
vis[n]=;
dis2[n]=;
while(!q2.empty())
{
int x=q2.front();
q2.pop();
for(int i=head2[x];i;i=e2[i].pre)
{
int y=e2[i].v;
if(dis2[y]>dis2[x]+e2[i].t)
{
dis2[y]=dis2[x]+e2[i].t;
if(!vis[y])
{
q2.push(y);
vis[y]=;
}
}
}
}
}
int main()
{
n=read();m=read();
for(int i=;i<=m;i++)
{
int x=read(),y=read(),z=read();
add1(i,x,y,z);add2(i,y,x,z);
}
spfa1();spfa2();
int cnt=;
for(int i=;i<=n;i++)
for(int j=head1[i];j;j=e1[j].pre)
f[++cnt]=dis1[i]+e1[j].t+dis2[e1[j].v];
int ans=INF,minn=INF;
for(int i=;i<=cnt;i++)
minn=min(minn,f[i]);
for(int i=;i<=cnt;i++)
if(f[i]>minn)ans=min(ans,f[i]);
if(ans<INF)printf("%d",ans);
else printf("-1");
return ;
}

匈牙利游戏(codevs 1269)的更多相关文章

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

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

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

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

  3. codevs——1269 匈牙利游戏

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

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

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

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

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

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

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

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

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

  8. codevs 1269 匈牙利游戏

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

  9. T1229 数字游戏 codevs

    http://codevs.cn/problem/1229/ 题目描述 Description Lele 最近上课的时候都很无聊,所以他发明了一个数字游戏来打发时间.  这个游戏是这样的,首先,他拿出 ...

随机推荐

  1. [转]IntelliJ IDEA 自定义方法注解模板

    IntelliJ IDEA 自定义方法注解模板 置顶2017年08月02日 18:04:36 阅读数:32592 最近没啥事开始正式用Eclipse 转入 idea工具阵营,毕竟有70%的开发者在使用 ...

  2. P3374 【模板】树状数组 1 单点修改与区间查询

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. ...

  3. JavaScript中,有三种常用的绑定事件的方法

    要想让 JavaScript 对用户的操作作出响应,首先要对 DOM 元素绑定事件处理函数.所谓事件处理函数,就是处理用户操作的函数,不同的操作对应不同的名称. 在JavaScript中,有三种常用的 ...

  4. codevs 5438 zbd之难题(水题日常)

     时间限制: 1 s  空间限制: 1000 KB  题目等级 : 白银 Silver 题目描述 Description zbd想要一个计算器,请你编一个计算器. 输入描述 Input Descrip ...

  5. Python 访问字典(dictionary)中元素

    访问python字典中元素的几种方式 一:通过“键值对”(key-value)访问: print(dict[key]) dict = {1: 1, 2: 'aa', 'D': 'ee', 'Ty': ...

  6. ajax请求的时候get 和post方式的区别

    ajax请求的时候get 和post方式的区别?一个在url后面一个放在虚拟载体里面有大小限制安全问题应用不同一个是论坛等只需要请求的,一个是类似修改密码的

  7. 如何快速上手基础的CSS3动画

    前言 说起CSS3动画,就必须说说 transform,translate,transition,animation这4个属性,transform主要定义元素的动作,比如旋转.倾斜.位移等,trans ...

  8. attr和prop的区别

    由于prop(property的缩写)和attr(attribute的缩写)翻译成汉语,均有“特性.属性”等意思的原因,导致大家容易混淆分不清. (1)在处理自定义时属性时,用attr(),若用pro ...

  9. CPP-基础:快速排序

    快速排序(Quicksort)是对冒泡排序的一种改进. 它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分 ...

  10. Hibernate5.x版本HQL限定查询 Legacy-style query parameters (`?`) are no longer supported

    在此版本的限定查询和4.0版本的限定查询: 如果查询语句是: String hql = "select u from User u where u.gender = ?"; 会出现 ...