loj 1099(最短路)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25956
思路:dist[v][0]代表走到点v的最短路,dist[v][1]代表走到点v的次短路(dist[v][0]!=dist[v][1]),然后Dijkstra更新就可以了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define MAXN 5555
#define MAXM 555555
#define inf 1<<30
#define FILL(a,b) memset(a,b,sizeof(a)) struct Edge{
int v,w,next;
}edge[MAXM]; int n,m,NE;
int head[MAXN]; void Insert(int u,int v,int w)
{
edge[NE].v=v;
edge[NE].w=w;
edge[NE].next=head[u];
head[u]=NE++;
} int dist[MAXN][];
int Dijkstra()
{
for(int i=;i<=n;i++)
for(int j=;j<=;j++)dist[i][j]=inf;
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >que;
que.push(make_pair(,));
dist[][]=;
while(!que.empty()){
pair<int,int>p=que.top();
que.pop();
for(int i=head[p.second];i!=-;i=edge[i].next){
int v=edge[i].v,w=edge[i].w;
if(p.first+w<dist[v][]){
dist[v][]=dist[v][];
dist[v][]=p.first+w;
que.push(make_pair(dist[v][],v));
}else if(p.first+w!=dist[v][]&&p.first+w<dist[v][]){
dist[v][]=p.first+w;
que.push(make_pair(dist[v][],v));
}
}
}
return dist[n][];
}
int main()
{
int _case,t=;
scanf("%d",&_case);
while(_case--){
scanf("%d%d",&n,&m);
NE=;
FILL(head,-);
while(m--){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
Insert(u,v,w);
Insert(v,u,w);
}
printf("Case %d: %d\n",t++,Dijkstra());
}
return ;
}
loj 1099(最短路)的更多相关文章
- loj 1379(最短路变形)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27087 思路:题目的意思是求S->T的所有路径中花费总和小于 ...
- loj#10078. 新年好(最短路)
题目: loj#10078. 新年好 解析: 亲戚只有五个,可以把它们看成2,3,4,5,6号点,分别跑最短路,记录一下距离,然后DFS一下 这题非常玄学,我开了一个\(12*12\)的数组,没有离散 ...
- Light oj 1099 - Not the Best 次短路
题目大意:求次短路. 题目思路:由于可能存在重边的情况所以不能采用邻接矩阵储存图,我用了邻接表来存图. 由起点S到终点E的次短路可能由以下情况组成: 1.S到v点的次短路 + v到E的距离 2.S到v ...
- LOJ#6354. 「CodePlus 2018 4 月赛」最短路[最短路优化建图]
题意 一个 \(n\) 个点的完全图,两点之间的边权为 \((i\ xor\ j)*C\) ,同时有 \(m\) 条额外单向路径,问从 \(S\) 到 \(T\) 的最短路. \(n\leq 10^5 ...
- LOJ#3087. 「GXOI / GZOI2019」旅行者(最短路)
题面 传送门 题解 以所有的感兴趣的城市为起点,我们正着和反着各跑一边多源最短路.记\(c_{0/1,i}\)分别表示正图/反图中离\(i\)最近的起点,那么对于每条边\((u,v,w)\),如果\( ...
- LOJ P3953 逛公园 NOIP dp 最短路 拓扑排序
https://www.luogu.org/problemnew/show/P3953 开o2过了不开o2re一个点...写法如题 顺便一提这道题在我校oj是a不了的因为我校土豆服务器速度奇慢1s时限 ...
- [BZOJ5109][LOJ #6252][P4061][CodePlus 2017 11月赛]大吉大利,今晚吃鸡!(最短路+拓扑排序+传递闭包+map+bitset(hash+压位))
5109: [CodePlus 2017]大吉大利,晚上吃鸡! Time Limit: 30 Sec Memory Limit: 1024 MBSubmit: 107 Solved: 57[Sub ...
- @loj - 6354@「CodePlus 2018 4 月赛」最短路
目录 @description@ @solution@ @accepted code@ @details@ @description@ 企鹅国中有 N 座城市,编号从 1 到 N . 对于任意的两座城 ...
- LOJ.6435.[PKUSC2018]星际穿越(倍增)
LOJ BZOJ 参考这儿qwq. 首先询问都是求,向左走的最短路. \(f[i][j]\)表示从\(i\)走到\(j\)最少需要多少步.表示这样只会\(O(n^2\log n)\)的= =但是感觉能 ...
随机推荐
- 学习Selenium2Library的好例子
最近好几个人问我有没有好的例子可以帮助学习Selenium2Library怎么用.对于公司同事,可以把脚本直接给过去,其他人则不行了.所以一直想做一个好的学习样例,这个样例应该有如下特性: 能够非常好 ...
- Html5 postMessage
解释: 跨文档消息传输Cross Document Messaging. 编写代码前注意判断浏览器是否支持Html5 实例: b页面向a页面发送消息. <!DOCTYPE> <htm ...
- ffmpeg-20160728-bin.7z
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...
- NSOJ A fairy tale of the two(最小费用最大流、SPFA版本、ZKW版本)
n,m<=20,给两个n×m布尔矩阵,每次操作可将第一个矩阵的2个相邻元素互换.输出最少操作次数使得两个矩阵完全一样. 比赛的时候想过按照二分图完美匹配的类似做法构图,不过想到边太多以及卡各种题 ...
- 【leetcode】 Scramble String (hard)★
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 【leetcode】Factorial Trailing Zeroes(easy)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 安装oracleASM
问题:oracleasm status不能启动 解决方案: [root@localhost ~]# oracleasm statusChecking if ASM is loaded: noCheck ...
- September 1st 2016 Week 36th Thursday
Everything is going on, but don't give up trying. 万事随缘,但不要放弃努力. There are numerous things that we ca ...
- !带有指针的类和struct赋值的本质 - host to device
//这个变量必须在while循环外面 //原因是当将loadModels[modelNum].g_3DModel[0]赋值给新建类后 //里面的数值拷贝过去了,而里头的指针只给了地址 //所以如果这个 ...
- 最小集合(51nod 1616)
A君有一个集合. 这个集合有个神奇的性质. 若X,Y属于该集合,那么X与Y的最大公因数也属于该集合. 但是他忘了这个集合中原先有哪些数字. 不过幸运的是,他记起了其中n个数字. 当然,或许会因为过度紧 ...