HDU-2680 Choose the best route 单向边+反向dijkstra

Posted COLORFUL

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU-2680 Choose the best route 单向边+反向dijkstra相关的知识,希望对你有一定的参考价值。

https://vjudge.net/problem/HDU-2680

题意:给出m条边及其起始点终点与长度,已知可以从w个起点出发,求到某个终点s的最短路径。注意是单向边。m<1e5,w<n<1000.

题解:若每个起点都kijkstra一遍时间复杂度为O((E+VlogV)*V),会TLE,想了一下,终点当初起点,反向建边就可以了

坑点:做图论习题一度因为没看到directed,directional,wa到怀疑dijkstra错了

ac代码,用的邻接表存图及优先队列dijkstra。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<queue>
#include<set>
#include<stdio.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, m, ts[1005];
//set<long long> ans;
vector< pair<int, int> > E[maxn];
int d[maxn];
void init() {
    for (int i = 0; i <maxn; i++) E[i].clear(), d[i] = 1e9;
}
int main()
{
    int s, t;
    while (cin >> n >> m >> s) {
        init();



        for (int i = 1; i <= m; i++) {
            int x, y, z;
            scanf("%d%d%d", &x, &y, &z);
            //E[x].push_back(make_pair(y, z));
            E[y].push_back(make_pair(x, z));

        }

        priority_queue<pair<int, int> > Q;
        d[s] = 0; Q.push(make_pair(-d[s], s));
        while (!Q.empty()) {
            int now = Q.top().second;
            Q.pop();

            for (int i = 0; i < E[now].size(); i++)
            {
                int v = E[now][i].first;
                if (d[v] > d[now] + E[now][i].second) {
                    d[v] = d[now] + E[now][i].second;

                    Q.push(make_pair(-d[v], v));
                }
            }
        }
        int ts;
        scanf("%d", &ts);
        int anss = 1e9;
        for (int i = 0; i < ts; i++) { scanf("%d", &t); anss = min(anss, d[t]); }


        if (anss == 1e9)cout << -1 << endl;
        else cout << anss << endl;
    }
}

 

以上是关于HDU-2680 Choose the best route 单向边+反向dijkstra的主要内容,如果未能解决你的问题,请参考以下文章

HDU-2680 Choose the best route 单向边+反向dijkstra

hdu-2680 Choose the best route---dijkstra+反向存图或者建立超级源点

Choose the best route Dijkstra

Choose the best route HDU杭电2680dijkstra算法 || SPFA

How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC

hdu 2680 最短路