Poj 3255(dijkstra求次短路)

简介: Poj 3255(dijkstra求次短路)

题目传送门

详情见《挑战程序设计竞赛》P109

代码如下:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#define endl '\n'
using namespace std;
typedef long long ll;
const int N = 5500, INF = 0x3f3f3f3f;
typedef pair<int, int> PII;
struct edge
{
  int to;
  int cost;
};
vector<edge>g[N];
int m, n;
int d1[N];
int dis2[N];
void dijkstra()
{
  priority_queue<PII, vector<PII>, greater<PII> >q;
  fill(d1 + 1, d1 + 1 + n, INF);
  fill(dis2 + 1, dis2 + 1 + n, INF);
  d1[1] = 0;
  q.push({0, 1});
  while (q.size())
  {
    PII t = q.top();
    q.pop();
    int v = t.second, d = t.first;
    if (dis2[v] < d) continue;
    for (int i = 0; i < g[v].size(); i++)
    {
      edge x = g[v][i];
      int d2 = d + x.cost;
      if (d1[x.to] > d2)
      {
        swap(d1[x.to], d2);
        q.push({d1[x.to], x.to});
      }
      if (dis2[x.to] > d2 && d2 > d1[x.to])
      {
        dis2[x.to] = d2;
        q.push({dis2[x.to], x.to});
      }
    }
  }
}
int main()
{
  while (~scanf("%d%d", &n, &m))
  {
    for (int i = 1; i <= m; i++)
    {
      int x, y, z;
      cin >> x >> y >> z;
      g[x].push_back({y, z});
      g[y].push_back({x, z});
    }
    dijkstra();
    cout << dis2[n] << endl;
    return 0;
  }
}


蔡珏
+关注
目录
打赏
0
0
0
0
2
分享
相关文章
POJ-2253,Frogger(最短路问题)
POJ-2253,Frogger(最短路问题)
poj 2229 Sumsets 【动态规划】
点击打开题目 Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 13291   Accepted: 5324 Description Far...
937 0
并查集-poj-1182
poj-1182-食物链 //2014.4.11 HDOJ携程编程大赛预赛第二场第一题 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形。A吃B, B吃C,C吃A。  现有N个动物,以1-N编号。每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种。  有人用两种说法对这N个动物所构成的食物链关系进行描述:  第一种说法是"1
1054 0
poj2031 kruskal
http://poj.org/problem?id=2031 #include&lt;iostream&gt; #include&lt;algorithm&gt; #include&lt;cmath&gt; #include&lt;cstdio&gt; using namespace std; double a[101][4]; double esp = 0.0000001;
1067 0
poj1287 kruskal
http://poj.org/problem?id=1287 #include&lt;cstdio&gt; #include&lt;cstdlib&gt; #include&lt;iostream&gt; #define N 60 using namespace std; struct node { int x,y; int len; } city[26
886 0