Poj3255 次短路

#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
typedef pair<int,int> P;
typedef long long LL;
typedef vector<int> V;
#define V(x) vector<x>

const int INF=0x3f3f3f3f,MOD=1000000000+7,maxn=5000+10;
int vis[205][205],E,s,v,cnt=1,m,n,ans=INF;
int par[maxn],dr[]={-1,1,0,0},dc[]={0,0,-1,1};

struct edge{
    int from,to,cost;
    edge(int a=0,int b=0,int c=0):from(a),to(b),cost(c){}
};
int find(int x) { return x == par[x] ? find(par[x]) : x; }
bool same(int x, int y) { x = find(x); y = find(y); return x == y; }
void unite(int x, int y) { x = find(x); y = find(y); if (x != y) par[x] = y; }
void init(int len) { for (int i = 0; i <= len; i++) par[i] = i; }

int d[maxn],d2[maxn];
V(edge) G[maxn];

void Dijkstra(){
    priority_queue<P,V(P),greater<P> > que;
    que.push(make_pair(0,1));
    d[1]=0;
    while (!que.empty()){
        P t=que.top(); que.pop();
        int v=t.second,di=t.first;
        if (d2[v]<di) continue;
 //       printf("di:%d v:%d\n",di,v);
        for (int i=0;i<G[v].size();i++){
            edge& e=G[v][i];
            int dis=di+e.cost;
            if (dis<d[e.to]) {
                swap(d[e.to],dis);
   //             printf("dis:%d d[%d]:%d\n",dis,e.to,d[e.to]);
                que.push(make_pair(d[e.to],e.to));
            }
         //   printf("d:");
            if (dis<d2[e.to]&&dis>d[e.to]){
     //           printf("dis:%d d2[%d]:%d\n",dis,e.to,d2[e.to]);
                d2[e.to]=dis;
                que.push(make_pair(d2[e.to],e.to));
            }
            /*for (int i=0;i<=n;i++){
                printf("%d ",d[i]);
            }putchar('\n');
            printf("d2:");
            for (int j=0;j<=n;j++){
                printf("%d ",d2[i]);
            }putchar('\n');*/
        }
    }
}

int main(){
    scanf("%d%d",&n,&m);
    fill(d,d+n+1,INF);
    fill(d2,d2+n+1,INF);
    for (int i=0;i<m;i++){
        int u,v,cos;
        scanf("%d%d%d",&u,&v,&cos);
        G[u].push_back(edge(u,v,cos));
        G[v].push_back(edge(v,u,cos));
    }
    Dijkstra();
    printf("%d\n",d2[n]);
   // printf("%d\n",d[n]);
}





 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值