http://codevs.cn/problem/1269/

 时间限制: 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.

严格次短路、

 #include <cstring>
#include <cstdio>
#include <queue> const int N(+);
const int M(+);
int n,hed[N],had[N],sumedge;
struct Edge
{
int v,next,w;
Edge(int v=,int next=,int w=):
v(v),next(next),w(w){}
}edge1[M],edge2[M];
inline void ins(int u,int v,int w)
{
edge1[++sumedge]=Edge(v,hed[u],w);
hed[u]=sumedge;
edge2[sumedge]=Edge(u,had[v],w);
had[v]=sumedge;
} bool inq[N];
int d1[N],d2[N];
void SPFA(int s,int *dis,int *head,Edge *edge)
{
std::queue<int>que;
for(int i=;i<=n;i++)
dis[i]=0x3f3f3f3f,inq[i]=;
dis[s]=; que.push(s);
for(int u,v;!que.empty();)
{
u=que.front(); que.pop(); inq[u]=;
for(int i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(dis[v]>dis[u]+edge[i].w)
{
dis[v]=dis[u]+edge[i].w;
if(!inq[v]) inq[v]=,que.push(v);
}
}
}
} inline void read(int &x)
{
x=; register char ch=getchar();
for(;ch>''||ch<'';) ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
} int AC()
{
int m;
read(n),read(m);
for(int u,v,w;m--;ins(u,v,w))
read(u),read(v),read(w);
SPFA(,d1,hed,edge1);
SPFA(n,d2,had,edge2);
int ans=0x3f3f3f3f;
for(int u=;u<=n;u++)
{
for(int v,i=hed[u];i;i=edge1[i].next)
{
v=edge1[i].v;
int tmp=d1[u]+d2[v]+edge1[i].w;
if(tmp<ans&&tmp>d1[n]) ans=tmp;
}
}
if(ans==0x3f3f3f3f) ans=-;
printf("%d\n",ans);
return ;
} int Hope=AC();
int main(){;}

AC的SPFA

下面可以忽略、

 #include <cstring>
#include <cstdio>
#include <queue> const int N(+);
const int M(+);
int hed[N],had[N],sumedge;
struct Edge
{
int v,next,w;
Edge(int v=,int next=,int w=):
v(v),next(next),w(w){}
}edge1[M],edge2[M];
inline void ins(int u,int v,int w)
{
edge1[++sumedge]=Edge(v,hed[u],w);
hed[u]=sumedge;
edge2[sumedge]=Edge(u,had[v],w);
had[v]=sumedge;
} int dis[N];
bool inq[N];
void SPFA(int s)
{
for(int i=;i<=s;i++) dis[i]=0x3f3f3f3f;
std::queue<int>que;
dis[s]=; que.push(s);
for(int u,v;!que.empty();)
{
u=que.front(); que.pop(); inq[u]=;
for(int i=had[u];i;i=edge2[i].next)
{
v=edge2[i].v;
if(dis[v]>dis[u]+edge2[i].w)
{
dis[v]=dis[u]+edge2[i].w;
if(!inq[v]) que.push(v),inq[v]=;
}
}
}
} struct Node
{
int now,g;
bool operator < (const Node x)const
{
return dis[now]+g>dis[x.now]+x.g;
}
};
int A_star(int s,int t,int k)
{
if(dis[s]==0x3f3f3f3f) return -;
int ret=0x3f3f3f3f,cnt=;
std::priority_queue<Node>que;
Node u,v; u.now=s; u.g=;
que.push(u);
for(;!que.empty();)
{
u=que.top(); que.pop();
if(cnt>k) return -;
if(u.now==t)
{
if(ret!=u.g)
{
ret=u.g;
if(++cnt==)
return ret;
}
}
for(int i=hed[u.now];i;i=edge1[i].next)
{
v.now=edge1[i].v;
v.g=u.g+edge1[i].w;
que.push(v);
}
}
return -;
} inline void read(int &x)
{
x=; register char ch=getchar();
for(;ch>''||ch<'';) ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
} int AC()
{
int n,m,k;
read(n),read(m);k=m;
for(int u,v,w;m--;ins(u,v,w))
read(u),read(v),read(w);
SPFA(n);
printf("%d\n",A_star(,n,k));
return ;
} int Hope=AC();
int main(){;}

爆栈的Astar

 #include <cstdio>
#include <queue> using namespace std; const int INF(0x3f3f3f3f);
const int N(+);
const int M(+); int m,n,head[N],sumedge;
struct Edge
{
int v,next;
long long w;
Edge(int v=,int next=,long long w=):
v(v),next(next),w(w){}
}edge[M<<];
inline void ins(int u,int v,long long w)
{
edge[++sumedge]=Edge(v,head[u],w);
head[u]=sumedge;
} struct Node
{
int now;
long long dis;
bool operator < (const Node &x) const
{
return dis>x.dis;
}
}; bool vis[N];
long long d1[N],d2[N];
#define swap(a,b) {long long tmp=a;a=b;b=tmp;}
inline void Dijkstar()
{
for(int i=;i<=n;i++) d1[i]=d2[i]=INF;
priority_queue<Node>que; Node u,to;
u.dis=d1[]=; vis[]=;
u.now=; que.push(u);
for(int dis,v;!que.empty();)
{
u=que.top();que.pop();
if(u.dis>d2[u.now]) continue;
for(int i=head[u.now];i;i=edge[i].next)
{
v=edge[i].v;
dis=u.dis+edge[i].w;
if(dis<d1[v])
{
swap(dis,d1[v]);
to.now=v;
to.dis=d1[v];
que.push(to);
}
if(dis>d1[v]&&dis<d2[v])
{
d2[v]=dis;
to.dis=d2[v];
to.now=v;
que.push(to);
}
}
}
} inline void read(int &x)
{
x=; register char ch=getchar();
for(;ch>''||ch<'';) ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
} int AC()
{ read(n),read(m);
for(int v,u,w;m--;ins(u,v,(long long)w))
read(u),read(v),read(w);
Dijkstar();
if(d2[n]==d1[n]) d2[n]=-;
printf("%lld\n",d2[n]);
return ;
} int I_want_AC=AC();
int main(){;}

不能输出-1的Dijkstra

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

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

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

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

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

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

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

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

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

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

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

  6. 【动态规划】【记忆化搜索】【搜索】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& ...

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

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

  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. JAVA循环迭代中删除或添加集合数据报java.util.ConcurrentModificationException错误

    1.写出下面的输出结果 public class test{ public static void main(String [] args) List<String> list = new ...

  2. memcached—向memcached中保存Java实体需注意的问题

    今天以代码实例的形式总结一下向memcached中保存Java实体需注意的问题: memcached工具类代码: package com.ghj.packageoftool; import java. ...

  3. Ant批量处理jmeter脚本

    Ant是一个可以把代码从某个地方拿来,编译,再拷贝到某个地方去的构建工具.一时冲动学习一下,顺便王婆卖瓜尝试着处理jmeter的脚本,于是,采坑之旅也从此开始.本文省略ant安装步骤和ant脚本说明, ...

  4. Linux环境thinkphp配置以及数据源驱动改动

    项目中须要用到thinkphp,以下简称tp. linux版本号:64位CentOS 6.4 Nginx版本号:nginx1.8.0 php版本号:php5.5.28 thinkphp版:3.2.3 ...

  5. web后台知识点整理

    五.JEE 适用于创建server端的大型的软件服务系统 1. JEE : JAVA  PLATFORM  ENTERPRISE  DEDITON 2.是一个规范集.技术集.框架集(API集) 一种技 ...

  6. codeforces 490 D Chocolate

    题意:给出a1*b1和a2*b2两块巧克力,每次可以将这四个数中的随意一个数乘以1/2或者2/3,前提是要可以被2或者3整除,要求最小的次数让a1*b1=a2*b2,并求出这四个数最后的大小. 做法: ...

  7. Oracle 11G R2 RAC中的scan ip 的用途和基本原理

    Oracle 11G R2 RAC增加了scan ip功能,在11.2之前,client链接数据库的时候要用vip,假如你的cluster有4个节点,那么客户端的tnsnames.ora中就对应有四个 ...

  8. Spark RDD概念学习系列之Pair RDD的分区控制

    不多说,直接上干货! Pair RDD的分区控制 Pair RDD的分区控制 (1) Spark 中所有的键值对RDD 都可以进行分区控制---自定义分区 (2)自定义分区的好处:  1) 避免数据倾 ...

  9. Ubuntu14.04下Mongodb数据库可视化工具安装部署步骤(图文详解)(博主推荐)

    不多说,直接上干货! 前期博客 Ubuntu14.04下Mongodb(离线安装方式|非apt-get)安装部署步骤(图文详解)(博主推荐) Ubuntu14.04下Mongodb官网安装部署步骤(图 ...

  10. SpringBoot(九) ElasticSearch 全文检索

    ElasticSearch ​ ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用 ...