题意:

无向图,求单源次短路,每条边可以走多次。

分析:

对于每个点记录最短路和次短路,次短路可以是由最短路+边或者是次短路+边更新而来。在更新每个点的最短路时,相应更新次短路,如果最短路更新了,就令次短路等于原来的最短路,如果没有,就看能否直接更新次短路。

代码:

#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
#define se second
#define fi first
typedef pair<int, int>pii;
struct edge{int to,w,next;};
const int maxm = 200010, maxn = 5005, INF = 0x3fffffff;
edge e[maxm];
int n,R;
int head[maxn], d[maxn], d2[maxn];
int dijkstra()
{
priority_queue<pii,vector<pii>,greater<pii> > q;
q.push(pii(0,1));
fill(d, d+n+1, INF);
fill(d2, d2+n+1, INF);
d[1] = 0;
while(!q.empty()){
pii tmp = q.top();q.pop();
int ne = head[tmp.se];
while(ne!=-1){
int v = e[ne].to;
int di = e[ne].w+tmp.fi;
int temp =di;
if(d[v] > di){
temp = d[v];
d[v]=di;
q.push(make_pair(d[v],v));
}
if(d2[v] > temp){
d2[v] = temp;
q.push(make_pair(d2[v],v));
}
ne = e[ne].next;
}
}
return d2[n];
}
int main (void)
{
scanf("%d%d",&n,&R);
int a, b, di;
fill(head, head+n+1,-1);
for(int i = 0; i < 2 * R; i++){
scanf("%d%d%d",&a,&b,&di);
e[i].to = b, e[i].w = di;
e[i].next = head[a];
head[a] = i++;
e[i].to = a, e[i].w = di;
e[i].next = head[b];
head[b] = i;
}
printf("%d\n",dijkstra());
}

看discuss有人说是道大水题,可是我感觉还不是很好想,可能还是对算法本身理解不彻底掌握不扎实吧。

POJ 3255_Roadblocks的更多相关文章

  1. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  2. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  3. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  4. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  5. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  6. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  7. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  8. POJ 2752 Seek the Name, Seek the Fame [kmp]

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17898   Ac ...

  9. poj 2352 Stars 数星星 详解

    题目: poj 2352 Stars 数星星 题意:已知n个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时 ...

随机推荐

  1. 一个小方法解决RGBA不兼容IE8

    原网页http://blog.csdn.net/leihope_/article/details/70158902 要在一个页面中设置一个半透明的白色div.这个貌似不是难题,只需要给这个div设置如 ...

  2. sql 关键字的用法

    coalesce( T.GoodsCode,'0') 若 T.GoodsCode 为NULL 这 用0替换 round(S.SaleEarning,2) 保留两位小数 SUBSTRING(zb.acc ...

  3. 微信小程序组件解读和分析:十、input输入框

    input输入框组件说明: 本文介绍input 输入框的各种参数及特性. input输入框示例代码运行效果如下: 下面是WXML代码: [XML] 纯文本查看 复制代码 ? 01 02 03 04 0 ...

  4. Prim算法以及Kruskal算法

    Prim算法主要用于计算最小生成树.算法在选取最小路径的时候需要优化,算法思路:从某个顶点开始,假设v0,此时v0属于最小生成树结点中的一个元素,该集合假设V,剩下的点待选择的点为U,然后找寻V中的点 ...

  5. Mysql——Innodb和Myisam概念与数据恢复

    Innodb和Myisam是Mysql常见的两种数据存储引擎.没有研究过Oracle.SQL Server等数据库,所以下面只针对Mysql. 一.两种方式的数据存储结构: 在Myisam下,数据库的 ...

  6. Youtube-dl 简短使用总结

    默认下载bestvideo+bestaudio,并通过ffmpeg -c copy output.mp4 简单的封装进mp4格式,而不进行转码. 有时候bestaudio 是opus编码的,但是mp4 ...

  7. 说说windows10自带浏览器Edge的好与不好

    用了10几个月了,正式版也升级了,今天来说说微软自带浏览器microsoft Edge的好与不好       先说好的吧     一,浏览器速度非常快,无论是打开还是关闭,或者是语音助手小娜需要调动浏 ...

  8. codeforces_1065_D.three pieces_思维

    题意:一个正方形棋盘,三种棋子,knight:像中国象棋中的马一样走:bishop:斜着走:rook:中国象棋中的车.棋盘中每个格子中标着1--n*n的互不相同的数字,从1开始任选一种棋子开始走,在每 ...

  9. Java集合(二)--Iterator和Iterable

    Iterable: public interface Iterable<T> { Iterator<T> iterator(); } 上面是Iterable源码,只有一个ite ...

  10. vue中的组件传值

    组件关系可以分为父子组件通信.兄弟组件通信.跨级组件通信. 父传子 - props 子传父 - $emit 跨级可以用bus 父子双向 v-model 父链(this.$parent this.$ch ...