poj 3255
code:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#include<string>
#include<queue>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<map>
#define inf 0x3f3f3f3f
#define LL long long
const int MAX=1e9+7;
const int MAXN=1e6+10;
const double pi=atan(1.0)*4;
using namespace std;
int dirx[4]= {0,1,0,-1};
int diry[4]= {1,0,-1,0};
int n,r;
typedef struct node{
int to,cost;
}edge;
vector<edge>graph[5010];
typedef pair<int,int> P;
int dis1[5010];
int dis2[5010];
void slove()
{
memset(dis1,inf,sizeof(dis1));
memset(dis2,inf,sizeof(dis2));
priority_queue <P,vector<P>,greater<P> > q;
dis1[1]=0;
q.push(P(0,1));
while(!q.empty())
{
P p=q.top();
q.pop();
int v=p.second,d=p.first;///first为起点到当前点的距离,second为edge结构体的to(当前点)
if(dis2[v]<d) continue;///当取出的值不是当前最短距离或次短距离,就舍弃他
for(int i=0;i<graph[v].size();i++)
{
edge e=graph[v][i];
int d2=d+e.cost;
if(dis1[e.to]>d2)
{
swap(dis1[e.to],d2);
q.push(P(dis1[e.to],e.to));
}
if(dis2[e.to]>d2&&dis1[v]<d2)
{
dis2[e.to]=d2;
q.push(P(dis2[e.to],e.to));
}
}
}
printf("%d\n",dis2[n]);
}
int main()
{
while(scanf("%d%d",&n,&r)!=EOF)
{
int a,b,d;
for(int i=0;i<r;i++)
{
scanf("%d%d%d",&a,&b,&d);
graph[a].push_back(edge{b,d});
graph[b].push_back(edge{a,d});
}
slove();
}
return 0;
}