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. zookeeper相关知识的总结:

    一.分布式协调技术 在给大家介绍ZooKeeper之前先来给大家介绍一种技术——分布式协调技术.那么什么是分布式协调技术?那么我来告诉大家,其实分布式协调技术 主要用来解决分布式环境当中多个进程之间的 ...

  2. MySql集合查询

    SELECT语句的查询结果是元组的集合,所以多个SELECT语句的结果可进行集合操作. 集合操作主要包括并操作UNION.交操作INTERSECT.差操作EXCEPT. 注意,参加集合操作的各查询结果 ...

  3. [原创]SQL表值函数:返回自定义时间段的日期数据

    跟以往类似,我依旧介绍一个我日常开发遇到的知识点,谨此记录一下,也希望能帮助到一些朋友. 这次我要介绍的是通过SQL函数返回你输入的两个时间点内的日期数据. 效果图如下: 执行函数:SELECT * ...

  4. PCLVisualizer可视化类(2)

    博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=163 可视化点云颜色特征 所示,点赋予不同的颜色表征其对应的z轴值不同.PC ...

  5. UVaLive 11525 Permutation (线段树)

    题意:有一个由1到k组成的序列,最小是1 2 … k,最大是 k k-1 … 1,给出n的计算方式,n = s0 * (k - 1)! + s1 * (k - 2)! +… + sk-1 * 0!, ...

  6. mooon模板的automake、autoconf、m4和libtool版本信息

    autoconf --version autoconf (GNU Autoconf) 2.59 automake --version automake (GNU automake) 1.9.6 m4 ...

  7. BMFont使用图片自定义字体(无需字体文件)

    网上搜BMFont做字体,很多都是从一个字体文件读取,然后选择需要的字,然后保存成图片文字,这个对于一般的文字的确很实用,因为Unity本身不支持中文,所以只好这样了. 但是做过游戏的都知道,策划总是 ...

  8. 【TMF eTOM】eTOM的概念和术语

    eTOM的概念 为了有效地理解和使用eTOM业务流程框架,我们首先要理解构成eTOM的关键概念.这些概念使eTOM成为集成业务流程设计/评估与传统过程的一个非常有效的工具.在这些概念中使用了在本文中详 ...

  9. MVC5手工添加System.Web.Optimization

    VS2012web手工添辑一个空的mvC5的项目,添加EF6的支持,在别的项目里复制了母版页_Layout.cshtml过来,发现Styles.Render未引用,这个引用使用的命名空间是System ...

  10. CodeForces - 906D Power Tower(欧拉降幂定理)

    Power Tower CodeForces - 906D 题目大意:有N个数字,然后给你q个区间,要你求每一个区间中所有的数字从左到右依次垒起来的次方的幂对m取模之后的数字是多少. 用到一个新知识, ...