题意:

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

分析:

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

代码:

#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. win7+idea+maven搭建spark源码阅读环境

    1.参考. 利用IDEA工具编译Spark源码(1.60~2.20) https://blog.csdn.net/He11o_Liu/article/details/78739699 Maven编译打 ...

  2. Asp.Net 设计模式 之 “简单工厂”模式

    主要思想: public static Operation CreateFactory(string ope)        {            //实例化空父类,让父类指向子类         ...

  3. 使用Win7 64位旗舰版光盘映像安装Windows Home basic 64位操作系统

    工作当中需要安装Windows home basic 64位操作系统,苦于手头没有该版本的安装光盘,也没时间下载其安装映像.因此,在现有资源“cn_windows_7_ultimate_with_sp ...

  4. mac webstrom 安装less

    1.检验电脑是否安装less lessc -v 2.如果没有执行全局安装命令 npm install -g less 3.webstrom -> Preferencs-> File Wat ...

  5. webstorm快捷键大全-webstorm常用快捷键

    默认配置下的常用快捷键,提高代码编写效率,离不开快捷键的使用,Webstorm拥有丰富的代码快速编辑功能,你可以自由配置功能快捷键. Webstorm预置了其他编辑器的快捷键配置,可以点击 查找/代替 ...

  6. chatops--rocketchat+hubot

    chatops--rocketchat+hubot 原文地址:http://www.cnblogs.com/caoguo/p/7221956.html 先放几张图 # rocket.chat # hu ...

  7. MIPS的寄存器、指令和寻址方式的分类

    MIPS的32个寄存器 助记符 编号 作用 zero 0 恒为0 at 1 (assembly temporary)保留给汇编器使用 v0,v1 2-3 (values)子程序返回,即函数调用时的返回 ...

  8. IIS HTTP 错误 401.3的解决办法

    目标网站添加新用户Everyone,选上需要的Everyone用户权限

  9. sqlserver 创建索引 物化 视图

    索引视图: create view  Test WITH SCHEMABINDING as select Id, Name from [dbo].[InterfaceCallSetting]creat ...

  10. Java中this、static关键字的内存图解

    Java中的关键字有很多,abstract  default  goto*  null  switch  boolean  do  if  package  nchronzed  break  dou ...