《挑战程序设计竞赛》 P108 Roadbloks 3255 改造Dijkstra算法 次短路径

次短路径

题目链接: http://poj.org/problem?id=3255

我求出了最短路径,要改造一下

#include <iostream>
#include  <algorithm>
#include <stdio.h>
#include <vector>
#include <queue>
#include  <utility>
#include <bits/stdc++.h>
using namespace std;
int N,R;
const  int  MM=5001;
const  int INF=0x3f3f3f3f;
struct  Edge{
      int to;
      int cost;

};

typedef pair<int ,int> P; //first : 最短距离  second : 目的点。



vector<Edge> G[MM];

int shortest[MM];
int  shorter[MM];

void dijks(){

      priority_queue< P, vector<P>, greater<P> > q;
      shortest[1]=0;
      q.push(P(0,1));


      while( !q.empty())
      {
            P p= q.top(); q.pop();
            int v=p.second;
            if( shortest[v] < p.first ) return;

            for(int i=0;i<G[v].size(); i++){
                  Edge &e = G[v][i];
                  if( shortest[ e.to ] > shortest[ v ] + e.cost ){
                        shortest[e.to] = shortest[ v ] + e.cost;
                        q.push( P( shortest[e.to] ,e.to) );
                        //cout << "入队" << shortest[e.to] <<" "<<e.to <<endl;
                  }
            }
      }
}


int main()

{
      cin >>  N >> R;

      for(int i=1;i<=R;i++){
            int a, b,c; cin >> a>> b >> c;

           Edge e; e.to= b;e.cost = c;

           Edge ee; ee.to=a; ee.cost =c;

           G[a].push_back(e);
           G[b].push_back(ee);
      }

      fill(shortest+1, shortest+N+1, INF );
      dijks();


      cout << *min_element(shortest+1, shortest+N+1 );

      for(int i=1;i<=N;i++){

            cout <<  i <<" "<< shortest[i] <<endl;
      }


    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值