题意:给出n个点,m条双向边,求严格次短路。

/*
先spfa预处理出起点到每个点的和每个点到终点的最短距离,然后枚举每条边(这条边必须走),计算此时的最短路径,得出严格次短路。
正确性:因为对于一条最短路,它不可能把所有的边走完(应该很显然),那么我们枚举所有边,就一定会枚举到它没走的边,此时的最短路就可能是次短路。
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#define N 5010
#define M 100010
#define INF 500000000
using namespace std;
int head[N],dis1[N],dis2[N],vis[N],n,m;
struct node{
int u,v,pre,t;
};node e[M*];
void add(int i,int x,int y,int z){
e[i].u=x;
e[i].v=y;
e[i].t=z;
e[i].pre=head[x];
head[x]=i;
}
void spfa1(int s){
memset(dis1,/,sizeof(dis1));
memset(vis,,sizeof(vis));
queue<int> q;
dis1[s]=;q.push(s);vis[s]=;
while(!q.empty()){
int x=q.front();q.pop();vis[x]=;
for(int i=head[x];i;i=e[i].pre){
if(dis1[e[i].v]>dis1[x]+e[i].t){
dis1[e[i].v]=dis1[x]+e[i].t;
if(!vis[e[i].v]){
vis[e[i].v]=;
q.push(e[i].v);
}
}
}
}
}
void spfa2(int s){
memset(dis2,/,sizeof(dis1));
memset(vis,,sizeof(vis));
queue<int> q;
dis2[s]=;q.push(s);vis[s]=;
while(!q.empty()){
int x=q.front();q.pop();vis[x]=;
for(int i=head[x];i;i=e[i].pre){
if(dis2[e[i].v]>dis2[x]+e[i].t){
dis2[e[i].v]=dis2[x]+e[i].t;
if(!vis[e[i].v]){
vis[e[i].v]=;
q.push(e[i].v);
}
}
}
}
}
void work(){
for(int i=;i<=m;i++){
int x,y,z;scanf("%d%d%d",&x,&y,&z);
add(i*-,x,y,z);add(i*,y,x,z);
}
spfa1();
spfa2(n);
int shortest=dis1[n],ci=INF;
for(int i=;i<=*m;i++){
int len=dis1[e[i].u]+dis2[e[i].v]+e[i].t;
if(len>shortest&&len<ci){
ci=len;
}
}
printf("%d\n",ci);
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
memset(head,,sizeof(head));
memset(e,,sizeof(e));
work();
}
return ;
}

Roadblocks(poj 3255)的更多相关文章

  1. 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)

    Charm Bracelet    POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...

  2. Scout YYF I(POJ 3744)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5565   Accepted: 1553 Descr ...

  3. 广大暑假训练1(poj 2488) A Knight's Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

  4. Games:取石子游戏(POJ 1067)

    取石子游戏 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37662   Accepted: 12594 Descripti ...

  5. BFS 或 同余模定理(poj 1426)

    题目:Find The Multiple 题意:求给出的数的倍数,该倍数是只由 1与 0构成的10进制数. 思路:nonzero multiple  非零倍数  啊. 英语弱到爆炸,理解不了题意... ...

  6. 并查集+关系的传递(poj 1182)

    题目:食物链 题意:给定一些关系.判断关系的正确性,后给出的关系服从之前的关系: 思路:难点不在并查集,在于关系的判断,尤其是子节点与根节点的关系的判断: 这个关系看似没给出,但是给出子节点与父节点的 ...

  7. 昂贵的聘礼(poj 1062)

    Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...

  8. Collecting Bugs(POJ 2096)

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 3064   Accepted: 1505 ...

  9. Power string(poj 2406)

    题目大意,给出一个字符串s,求最大的k,使得s能表示成a^k的形式,如 abab 可以表示成(ab)^2: 方法:首先 先求kmp算法求出next数组:如果 len mod (len-next[len ...

随机推荐

  1. Handler Should be static or leaks Occur?

    解决办法: public class SampleActivity extends Activity { /** * Instances of static inner classes do not ...

  2. CSS Image Sprite--网页图片应用处理方式

    CSS Sprites简介 CSSSprites在国内很多人叫css精 灵,是一种网页图片应用处理方式.它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片 ...

  3. Interleaving String leetcode

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  4. Android 数据存储之 文件存储

    -------------------------------------------文件存储----------------------------------------------- 文件存储是 ...

  5. Struts2中关于"There is no Action mapped for namespace / and action name"的总结

    今天在调试一个基础的Struts2框架小程序.总是提示"There is no Action mapped for namespace / and action name"的错误. ...

  6. python的变量作用域问题

    偶然掉进了一个坑里.仔细分析了下原因.原来是变量作用域的问题.简单抽象如下: id=1 #许多行代码 [id for id in range(10)] #许多行代码 if id!=1: #做一些事情 ...

  7. Zebra_Form Packages: Zebra_Form Controls Generic XSS_Clean Classes: Zebra_Form_Control Class: Zebra_Form_Control

    http://stefangabos.ro/wp-content/docs/Zebra_Form/Generic/Zebra_Form_Control.html#methodset_rule

  8. 烂泥:php5.6源码安装及php-fpm配置

    LNMP环境的搭建中,现在只有php没有源码安装过.这篇文章就把这个介绍下. 注意本篇文章使用的centos 6.5 64bit. 登陆centos下载php5.6的安装包.php的软件包可以去国内的 ...

  9. Eclipse CDT “Symbol NULL could not be resolved”

    在ubuntu里装的eclipse C/C++版,交叉编译程序时,总是提示Symbol NULL could not be resolved.Symbol size_t could not be re ...

  10. 【leetcode】Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...