POJ 3255_Roadblocks
题意:
无向图,求单源次短路,每条边可以走多次。
分析:
对于每个点记录最短路和次短路,次短路可以是由最短路+边或者是次短路+边更新而来。在更新每个点的最短路时,相应更新次短路,如果最短路更新了,就令次短路等于原来的最短路,如果没有,就看能否直接更新次短路。
代码:
#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的更多相关文章
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- 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 ...
- poj 2352 Stars 数星星 详解
题目: poj 2352 Stars 数星星 题意:已知n个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时 ...
随机推荐
- Windowsforms 中对文件操作
文件及文件夹操作: 引用命名空间:using system .IO; 1.File类: 创建:File.Create(路径);——返回FileStream FileStream fs = File.C ...
- ADO.net数据访问
需要引用对应命名空间:System.Data.SqlClient; SqlConnection:连接对象SqlCommand:命令对象SqlDataReader:读取器对象 //造连接字符串 stri ...
- ES6学习笔记(1)----let和const命令
参考书<ECMAScript 6入门>http://es6.ruanyifeng.com/ let和const命令 let 总结1.声明变量基本使用方法与var 相同 不同点 a.在代 ...
- Activity的四种启动模式区别
(1) standard 模式启动模式,每次激活Activity时都会创建Activity,并放入任务栈中. (2) singleTop 如果在任务的栈顶正好存在该Activity的实例, 就重用该实 ...
- 键盘工具栏的快速集成--IQKeyboardManager
转自:http://www.cnblogs.com/gaoxiaoniu/p/5333187.html 键盘工具栏的快速集成--IQKeyboardManager IQKeyboardManager, ...
- 【java_设计模式】建造者模式
应用场景对生成对象进行链式配置好处:按需配置对象,灵活修改默认配置,比构造方法创建对象更直观. public static void init() {if (okHttpClient == null) ...
- 生产者-消费者中的缓冲区:BlockingQueue接口
BlockingQueue接口使用场景相信大家对生产者-消费者模式不陌生,这个经典的多线程协作模式,最简单的描述就是生产者线程往内存缓冲区中提交任务,消费者线程从内存缓冲区里获取任务执行.在生产者-消 ...
- apm - 查询高级电源管理(APM) BIOS
总览 apm [ - vVmsS ] 描述 apm 读取 /proc/apm 并用人能看懂的格式输出.因为提供了首要的电池状态,这个命令在有兼容的 APM BIOS 的笔记本电脑上非常有用. apm ...
- samba的安装及其使用
参考pdf文档:https://pan.baidu.com/s/1iPJ1iPtNx7ZXNHRurrjfqw centos7 samba配置#centos7安装Sambayum -y install ...
- 机器学习_决策树Python代码详解
决策树优点:计算复杂度不高,输出结果易于理解,对中间值的缺失不敏感,可以处理不相关特征数据: 决策树缺点:可能会产生过度匹配问题. 决策树的一般步骤: (1)代码中def 1,计算给定数据集的香农熵: ...