【最短路】LightOJ 1099 - Not the Best 第二短路
来源:互联网 发布:mysql商品表设计 编辑:程序博客网 时间:2024/05/21 00:15
点击打开链接
题意:求第二短路,题目定义:一定要大于最短路 并且路可以重复走;
开二维数组 d[N][2] 分别记录最短路和第二短路
#include <cstdio>#include <cstring>#include <cstdlib>#include <string>#include <iostream>#include <algorithm>#include <sstream>#include <cmath>using namespace std;#include <queue>#include <stack>#include <set>#include <vector>#include <deque>#include <map>#define cler(arr, val) memset(arr, val, sizeof(arr))#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;const int MAXN = 5010+1;const int MAXM = 240000;const int INF = 0x3f3f3f3f;const int mod = 1000000007;int d[MAXN][2],head[MAXN],tol;bool vis[MAXN][2];struct node{ int u,v,val,next;}edge[MAXM];void addedge(int u,int v,int w){ edge[tol].u=u,edge[tol].v=v,edge[tol].val=w,edge[tol].next=head[u]; head[u]=tol++; edge[tol].u=v,edge[tol].v=u,edge[tol].val=w,edge[tol].next=head[v]; head[v]=tol++;}void dij(int n){ for(int i=0;i<=n;i++) d[i][0]=d[i][1]=INF,vis[i][0]=vis[i][1]=false; d[0][0]=0; while(true) { int v=-1,k; int minlen=INF; for(int u=0;u<n;u++) { for(int l=0;l<2;l++) if(!vis[u][l]&&(v==-1||d[u][l]<minlen)) { minlen=d[u][l]; k=l; v=u; } } if(v==-1) break; vis[v][k]=true; for(int i=head[v];~i;i=edge[i].next) { int u=edge[i].v;//目标 int cost=d[v][k]+edge[i].val; if(cost<d[u][0]) { d[u][1]=d[u][0]; d[u][0]=cost; } else if(cost<d[u][1]&&cost>d[u][0]) { d[u][1]=cost; } } }}int main(){#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout);#endif int t,n,m,u,v,w,cas=1; cin>>t; while(t--) { cler(head,-1); tol=0; cin>>n>>m; int a=0; for(int i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&w); a=max(a,u); a=max(a,v); u--,v--; addedge(u,v,w); } dij(a); printf("Case %d: %d\n",cas++,d[n-1][1]); } return 0;}
0 0
- 【最短路】LightOJ 1099 - Not the Best 第二短路
- lightoj 1099 - Not the Best (次短路)
- LightOJ - 1099 Not the Best(次短路)
- [第2短路变形]LightOJ 1099 - Not the Best
- LightOJ 1099 - Not the Best(Dijkstra次短路)
- LightOj 1099 Not the Best(次短路)
- lightoj 1099 - Not the Best(次短路)
- uva 10740 Not the Best (最短路 A*算法)
- (intermediate) 最短路(第k短路) UVA 10740 - Not the Best
- hdu2680最短路Choose the best route
- HDU2680 Choose the best route 最短路
- LightOJ-1099-spaf,次最短路
- lightoj 1174 - Commandos(最短路)
- uva 10740 - Not the Best(第k短路)
- 【最短路+dijkstra】 2680 Choose the best route
- hdu Choose the best route(最短路 --- Dijkstra)
- hdu 2680-Choose the best route最短路dijkstra
- Hud 2680 Choose the best route[基础最短路]
- 总线(bus)、设备(device)、驱动(driver)三者构成了设备驱动的模型
- 【BZOJ1003】物流运输trans
- C++排序算法代码
- docker 常用命令
- 总结一下c++(新手)需要注意的地方
- 【最短路】LightOJ 1099 - Not the Best 第二短路
- UVa 246 - 10-20-30(模拟+判重)
- jQuery中调用JSON和XML数据--jQuery基础知识点(6)
- c++读取bmp图片详解
- Android性能优化篇:从缓存管理进行优化
- vimrc中几个常用到的功能(初级)
- 民间借贷利率调查?[互联网金融P2P网贷应该做点什么的~]
- uva--103Stacking Boxes +dp
- codevs1040 统计单词个数(区间dp+划分dp)