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

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

题解:若每个起点都dijkstra一遍时间复杂度为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 + ;
int n, m, ts[];
//set<long long> ans;
vector< pair<int, int> > E[maxn];
int d[maxn];
void init() {
for (int i = ; i <maxn; i++) E[i].clear(), d[i] = 1e9;
}
int main()
{
int s, t;
while (cin >> n >> m >> s) {
init(); for (int i = ; 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] = ; Q.push(make_pair(-d[s], s));
while (!Q.empty()) {
int now = Q.top().second;
Q.pop(); for (int i = ; 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 = ; i < ts; i++) { scanf("%d", &t); anss = min(anss, d[t]); } if (anss == 1e9)cout << - << endl;
else cout << anss << endl;
}
}

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

  1. hdu 2680 Choose the best route (dijkstra算法 最短路问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS ( ...

  2. hdu 2680 Choose the best route

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...

  3. hdu 2680 Choose the best route (dijkstra算法)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2680 /************************************************* ...

  4. hdu 2680 Choose the best route 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不 ...

  5. HDU 2680 Choose the best route(SPFA)

    Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...

  6. HDU 2680 Choose the best route 最短路问题

    题目描述:Kiki想去他的一个朋友家,他的朋友家包括所有的公交站点一共有n 个,一共有m条线路,线路都是单向的,然后Kiki可以在他附近的几个公交站乘车,求最短的路径长度是多少. 解题报告:这道题的特 ...

  7. HDU 2680 Choose the best route(多起点单终点最短路问题)题解

    题意:小A要乘车到s车站,他有w个起始车站可选,问最短时间. 思路:用Floyd超时,Dijkstra遍历,但是也超时.仔细看看你会发现这道题目好像是多源点单终点问题,终点已经确定,那么我们可以直接转 ...

  8. HDU 2068 Choose the best route

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...

  9. hdoj 2680 choose the best route

    Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsicknes ...

随机推荐

  1. 在Ubuntu中开启Soft AP功能

    在Ubuntu中开启Soft AP功能 1.查看采用的无线网卡是否支持Soft AP: 注意,可以看到有AP字样,表明支持.楼主比较背,在易迅上挑了个销量最高的netcore nw360,结果无法搭建 ...

  2. c#事件Unity与.Net对比

    今天在看Unity3d的书,发现上面的调用事件比较特殊,比如说按钮事件 该方法写在OnGUI方法中if(GUILayout.Button("按钮1")) { //执行事件的处理 } ...

  3. 02python程序和用户交互

    在写程序时,使用python的内置函数来获取用户输入的值. >>> name = input("Input your name:")Input your name ...

  4. 【RF库Collections测试】lists should be equal

    场景一:msg=None 场景二:自定义msg 场景三:自定义msg和values,且values为布尔类型False或者字符串False和No Values 场景四:自定义msg和values,且v ...

  5. mybatis 之 parameterType="Map"

    // 获得品牌下的商品 Map<String, Object> params = new HashMap<String, Object>(); params.put(" ...

  6. JLINK与JTAG的区别(转)

    调试ARM,要遵循ARM的调试接口协议,JTAG就是其中的一种.当仿真时,IAR.KEIL.ADS等都有一个公共的调试接口,RDI就是其中的一种,那么我们如何完成RDI-->ARM调试协议(JT ...

  7. 剑指offer面试题7:用两个栈实现队列

    题目1:用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 代码实现: public class Solution07 { Stack<Integer> ...

  8. 非static成员函数通过类名::来调用,空指针调用成员方法不出错!

    首先来看这一段代码: #include <iostream> using namespace std; class A{ public: int k; void p1(){ cout< ...

  9. Bypassing PatchGuard on Windows x64

    [说明] 1.  本文是意译,加之本人英文水平有限.windows底层技术属菜鸟级别,本文与原文存在一定误差,请多包涵. 2.  由于内容较多,从word拷贝过来排版就乱了.故你也可以下载附件. 3. ...

  10. jQuery缓存机制(三)

    缓存机制提供的入口有: $.data([key],[value]) // 存取数据 $.hasData(elem) // 是否有数据 $.removeData([key]) // 删除数据 $.acc ...