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,然后枚举起点终点(所必走的边),取次小,因为是严格次短,所以可以这样YY掉

 #include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 100000+10
#define INF 0x7fffffff using namespace std; struct ss
{
int to,next,edge;
}e_t[maxn<<],e_f[maxn<<]; struct sss
{
int t,f;
void add(int x)
{
t=x; f=x;
}
}dis[maxn]; int head_t[maxn],head_f[maxn],cnt_t,cnt_f,vis[maxn]; void insert_t(int u,int v,int edge)
{
e_t[++cnt_t].next=head_t[u];
e_t[cnt_t].to=v;
e_t[cnt_t].edge=edge;
head_t[u]=cnt_t;
} void insert_f(int u,int v,int edge)
{
e_f[++cnt_f].next=head_f[u];
e_f[cnt_f].to=v;
e_f[cnt_f].edge=edge;
head_f[u]=cnt_f;
} void spfa_t(int x)
{
queue<int>que;
que.push(x);
vis[x]=;dis[x].t=;
while (!que.empty())
{
int now=que.front();que.pop();vis[now]=;
for (int i=head_t[now];i;i=e_t[i].next)
if (dis[e_t[i].to].t>dis[now].t+e_t[i].edge)
{
dis[e_t[i].to].t=dis[now].t+e_t[i].edge;
if (!vis[e_t[i].to]) que.push(e_t[i].to),vis[e_t[i].to]=;
}
}
} void spfa_f(int x)
{
queue<int>que;
que.push(x);
vis[x]=;dis[x].f=;
while (!que.empty())
{
int now=que.front();que.pop();vis[now]=;
for (int i=head_f[now];i;i=e_f[i].next)
if (dis[e_f[i].to].f>dis[now].f+e_f[i].edge)
{
dis[e_f[i].to].f=dis[now].f+e_f[i].edge;
if (!vis[e_f[i].to]) que.push(e_f[i].to),vis[e_f[i].to]=;
}
}
} int n,m; int main()
{
scanf("%d%d",&n,&m);
for (int i=;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
insert_t(x,y,z),insert_f(y,x,z);
}
for (int i=;i<=n;i++) dis[i].add(INF);
spfa_t();spfa_f(n);
int ans=INF,sans=-;
for (int i=;i<=n;i++)
for (int j=head_t[i];j;j=e_t[j].next)
{
if (dis[i].t!=INF&&dis[e_t[j].to].f!=INF&&ans>dis[e_t[j].to].f+dis[i].t+e_t[j].edge)
sans=ans,ans=dis[e_t[j].to].f+dis[i].t+e_t[j].edge;
if (dis[i].t!=INF&&dis[e_t[j].to].f!=INF&&ans<dis[e_t[j].to].f+dis[i].t+e_t[j].edge&&dis[e_t[j].to].f+dis[i].t+e_t[j].edge<sans)
sans=dis[e_t[j].to].f+dis[i].t+e_t[j].edge;
}
if (sans==INF) cout<<-<<endl; else cout<<sans<<endl;
return ;
}

C++之路进阶codevs1269(匈牙利游戏)的更多相关文章

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

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

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

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

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

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

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

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

  5. 匈牙利游戏(codevs 1269)

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

  6. codevs——1269 匈牙利游戏

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

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

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

  8. C++之路进阶——bzoj1455(罗马游戏)

    F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser  gryz2016 Logout 捐赠本站 Notice:由于本OJ ...

  9. Photon服务器进阶&一个新游戏的出产(一)

    许久没写了,今日闲来无事随性一些,总结一下最近的游戏经历(写游戏) Photon服务器的进阶~ 距离上一篇估计也过去一个多月了,对于Photon真是又爱又恨. 爱着它的书写方便. 恨着它的教程稀少总走 ...

随机推荐

  1. AngularJs表单验证

    常用的表单验证指令 1. 必填项验证 某个表单输入是否已填写,只要在输入字段元素上添加HTML5标记required即可: <input type="text" requir ...

  2. jQuery Live Query 插件

    http://www.cnblogs.com/sunjing/archive/2008/12/06/1349097.html 上篇日志里写到的那个问题其实有种更简单的解决办法,就是使用jquery的L ...

  3. nginx中配置pathinfo模式示例

    要想让nginx支持PATH_INFO,首先需要知道什么是pathinfo,为什么要用pathinfo? pathinfo不是nginx的功能,pathinfo是php的功能. php中有两个path ...

  4. 从零开始山寨Caffe·叁:全局线程管理器

    你需要一个管家,随手召唤的那种,想吃啥就吃啥. ——设计一个全局线程管理器 一个机器学习系统,需要管理一些公共的配置信息,如何存储这些配置信息,是一个难题. 设计模式 MVC框架 在传统的MVC编程框 ...

  5. poj1236Network of Schools Tarjan裸题

    其实就是手打了个Tarjan的模板 输出的时候注意是入度为0的点的个数和max(入度0的个数,出度0的个数),在n=1时特判为0即可 ——以后图论要渐渐模板化,方便使用 #include <cs ...

  6. 自己封装一个Log模块

    Unity自己有log系统,为什么要自己封装一个 1.不好用,只能在pc上记录log文件,移动平台是没有的 2.在开发时期的log,不想在正式版里面出现.没有一个统一的开关来控制是不是要显示log,要 ...

  7. Odoo domain 中的 like, ilike, =like, =ilike 举例说明【转】

    Odoo domain 中的 like, ilike, =like, =ilike 举例说明 Odoo domain 操作符使用场景非常多,很多小伙伴被 like, ilike, =like, =il ...

  8. *HDU1969 二分

    Pie Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  9. Javascript学习之无缝滚动

    无缝滚动Javascript: <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g ...

  10. java面向对象_抽象类和接口

    一.抽象类 1.抽象方法:由abstract修饰.只有定义没有方法体.用一个分号结尾. 2.抽象类: 1)包含抽象方法的类必须是抽象类 2)由abstract修饰 3)不能被实例化 4)抽象类如果不被 ...