poj3255 次短路
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N = 1e5 + 10;
int n,m;
int dis1[N],dis2[N];
struct node{
int v,w;
node(){}
node(int vv,int ww){v = vv;w=ww;}
bool operator < (const node &b)const{
return w > b.w;
}
};
vector<node>q[N];
void dj()
{
priority_queue<node>qq;
memset(dis1,0x3f,sizeof dis1);
memset(dis2,0x3f,sizeof dis2);
dis1[1] = 0;
qq.push(node(1,0));
while(!qq.empty()){
node x = qq.top();
qq.pop();
int v = x.v,d = x.w;
if(dis2[v] < d)continue;
for(int i = 0; i < q[v].size(); i++){
node y = q[v][i];
int d2 = d + y.w;
if(dis1[y.v] > d2){
swap(dis1[y.v],d2);
qq.push(node(y.v,dis1[y.v]));
}
if(dis2[y.v] > d2 && dis1[y.v] < d2){
dis2[y.v] = d2;
qq.push(node(y.v,dis2[y.v]));
}
}
}
cout << dis2[n] << endl;
}
int main()
{
freopen("in.txt","r+",stdin);
ios::sync_with_stdio(false);
while(cin >> n >> m){
for(int i = 0; i < N; i++)q[i].clear();
for(int i = 1; i <= m; i++){
int a,b,c;
cin >> a >> b >> c;
q[a].push_back(node(b,c));
q[b].push_back(node(a,c));
}
dj();
}
return 0;
}