poj 3255 Roadblocks
Roadblocks
Time Limit: 2000MS |
Memory Limit: 65536K |
|
Total Submissions: 13216 |
Accepted: 4660 |
Description
Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.
The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersectionN.
The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).
Input
Line 1: Two space-separated integers: N and R
Lines 2..R+1: Each line contains three space-separated integers: A, B,
and D that
describe a road that connects intersections A and B and
has length D (1
≤ D ≤
5000)
Output
Line 1: The length of
the second shortest path between node 1 and node N
Sample Input
4 4
1 2 100
2 4 200
2 3 250
3 4 100
Sample Output
450
Hint
Two routes: 1 -> 2 ->
4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)
Source
题意:
给出n个点,m条双向边,求严格次短路。
AC代码:
- #include<cstdio>
- #include<cstring>
- #include<queue>
- #define R register
- using namespace std;
- inline int read(){
- R int x=;bool f=;
- R char ch=getchar();
- while(ch<''||ch>''){if(ch=='-')f=;ch=getchar();}
- while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
- return f?x:-x;
- }
- const int N=1e5+;
- struct node{
- int u,v,w,next;
- }e[N<<];
- int n,m,tot,head[N],dis1[N],dis2[N];
- bool vis[N];
- void add(int x,int y,int z){
- e[++tot].u=x;
- e[tot].v=y;
- e[tot].w=z;
- e[tot].next=head[x];
- head[x]=tot;
- }
- void spfa1(int S){
- queue<int>q;
- memset(vis,,sizeof vis);
- memset(dis1,/,sizeof dis1);
- q.push(S);
- dis1[S]=;vis[S]=;
- while(!q.empty()){
- int x=q.front();q.pop();
- vis[x]=;
- for(int i=head[x];i;i=e[i].next){
- int v=e[i].v,w=e[i].w;
- if(dis1[v]>dis1[x]+w){
- dis1[v]=dis1[x]+w;
- if(!vis[v]){
- vis[v]=;
- q.push(v);
- }
- }
- }
- }
- }
- void spfa2(int S){
- queue<int>q;
- memset(vis,,sizeof vis);
- memset(dis2,/,sizeof dis2);
- q.push(S);
- dis2[S]=;vis[S]=;
- while(!q.empty()){
- int x=q.front();q.pop();
- vis[x]=;
- for(int i=head[x];i;i=e[i].next){
- int v=e[i].v,w=e[i].w;
- if(dis2[v]>dis2[x]+w){
- dis2[v]=dis2[x]+w;
- if(!vis[v]){
- vis[v]=;
- q.push(v);
- }
- }
- }
- }
- }
- void Cl(){
- tot=;
- memset(e,,sizeof e);
- memset(head,,sizeof head);
- }
- void work(){
- Cl();
- for(int i=,x,y,z;i<=m;i++){
- x=read();y=read();z=read();
- add(x,y,z);
- add(y,x,z);
- }
- spfa1();
- spfa2(n);
- int shortest=dis1[n],shorter=0x7fffffff;
- for(int i=;i<=m*;i++){
- int len=dis1[e[i].u]+dis2[e[i].v]+e[i].w;
- if(len>shortest&&len<shorter) shorter=len;
- }
- printf("%d\n",shorter);
- }
- int main(){
- while(scanf("%d%d",&n,&m)==) work();
- return ;
- }
poj 3255 Roadblocks的更多相关文章
- POJ 3255 Roadblocks(A*求次短路)
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12167 Accepted: 4300 Descr ...
- POJ 3255 Roadblocks (次级短路问题)
解决方案有许多美丽的地方.让我们跳回到到达终点跳回(例如有两点)....无论如何,这不是最短路,但它并不重要.算法能给出正确的结果 思考:而最短的路到同一点例程.spfa先正达恳求一次,求的最短路径的 ...
- POJ 3255 Roadblocks (次短路模板)
Roadblocks http://poj.org/problem?id=3255 Time Limit: 2000MS Memory Limit: 65536K Descriptio ...
- 次最短路径 POJ 3255 Roadblocks
http://poj.org/problem?id=3255 这道题还是有点难度 要对最短路径的算法非常的了解 明晰 那么做适当的修改 就可以 关键之处 次短的路径: 设u 到 v的边权重为cost ...
- poj 3255 Roadblocks 次短路(两次dijksta)
Roadblocks Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total S ...
- POJ 3255 Roadblocks (Dijkstra求最短路径的变形)(Dijkstra求次短路径)
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16425 Accepted: 5797 Descr ...
- POJ 3255 Roadblocks --次短路径
由于次短路一定存在,则可知次短路一定是最短路中某一条边不走,然后回到最短路,而且只是一条边,两条边以上不走的话,就一定不会是次短路了(即以边换边才能使最小).所以可以枚举每一条边,算出从起点到这条边起 ...
- POJ 3255 Roadblocks (次短路 SPFA )
题目链接 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her ...
- POJ 3255 Roadblocks (次短路)
题意:给定一个图,求一条1-n的次短路. 析:次短路就是最短路再长一点呗,我们可以和求最短路一样,再多维护一个数组,来记录次短路. 代码如下: #pragma comment(linker, &quo ...
随机推荐
- 转载文章----初识Ildasm.exe——IL反编译的实用工具
转载地址http://www.cnblogs.com/yangmingming/archive/2010/02/03/1662307.html Ildasm.exe 概要:(路径:C:\Program ...
- 【转发】网易邮箱前端技术分享之javascript编码规范
网易邮箱前端技术分享之javascript编码规范 发布日期:2013-11-26 10:06 来源:网易邮箱前端技术中心 作者:网易邮箱 点击:533 网易邮箱是国内最早使用ajax技术的邮箱.早在 ...
- [20130704] Intra-Query Parallel Thread Deadlocks
今天碰到了 Intra-Query Parallel Thread Deadlocks 简单的说就是并发查询把自己给锁住了. 原理: 在并发查询运行是,有一个生产者和一个消费者,生产者等待消费者产生 ...
- react native windows开发环境搭建(一)
ReactNative分为服务器端和手机端loader程序,Android版有3种代码:js代码,java代码和c++代码,主要是编写的是js代码,如果框架功能不足就需要编写原生的java代码来扩展, ...
- Linux 多线程信号量同步
PV原子操作 P操作: 如果有可用的资源(信号量值>0),则此操作所在的进程占用一个资源(此时信号量值减1,进入临界区代码); 如果没有可用的资源(信号量值=0),则此操作所在的进程被阻塞直到系 ...
- CentOS 6.3下配置LVM(逻辑卷管理)
一.简介 LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵 ...
- namenode metadata 备份与恢复实验
https://hadoop.apache.org/docs/r2.6.0/hadoop-project-dist/hadoop-hdfs/HDFSCommands.html#dfsadmin -me ...
- Windows Azure支持七层负载均衡--Application Gateway
一直以来Windows Azure的负载均衡(Loadbalancer)功能一直被客户诟病,无法其竞争对手(特别是国内的云厂商)匹敌. Windows Azure的负载均衡器是四层的,前期的版本不支持 ...
- Uva 11395 Sigma Function (因子和)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=109329#problem/C 题目在文末 题意:1~n (n:1~1012)中,因子 ...
- MyBatis使用总结+整合Spring
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .20 ...