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;
    }
}

原文地址:https://www.cnblogs.com/SuuT/p/8524648.html

时间: 2024-10-11 18:57:14

HDU-2680 Choose the best route 单向边+反向dijkstra的相关文章

HDU - 2680 - Choose the best route (经典最短路问题dijkstra算法!!)

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7671    Accepted Submission(s): 2524 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

HDU 2680 Choose the best route &lt;SPFA算法+反向建图&gt;

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10690    Accepted Submission(s): 3454 Problem Description One day , Kiki wants to visit one of her friends. As she is liabl

hdu 2680 Choose the best route 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不要写成 map[i][j] = map[j][i] = X. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 using namespac

HDU 2680 Choose the best route(最短路)

Choose the best route Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2680 Description One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at

hdu 2680 Choose the best route (Dijkstra &amp; 反向图)

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7764    Accepted Submission(s): 2581 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

hdu 2680 Choose the best route 大年三十的首A 赤裸裸的Dijkstra 做这题需要一个小技巧

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8063    Accepted Submission(s): 2655 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

HDU 2680 Choose the best route(dijkstra+优先队列优化)

Choose the best route Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stati

HDU 2680 Choose the best route (最短路)

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7361    Accepted Submission(s): 2408 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

hdu 2680 Choose the best route

BFS..... #include<stdio.h> #include<math.h> #include<string.h> #include<vector> #include<algorithm> using namespace std; const int maxn = 1111; vector<int>abc[maxn]; int ji[maxn], tt[maxn][maxn], yy[maxn]; struct aaa{ i