HDU-2680 Choose the best route 单向边+反向dijkstra
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的更多相关文章
- 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 ( ...
- hdu 2680 Choose the best route
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...
- hdu 2680 Choose the best route (dijkstra算法)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2680 /************************************************* ...
- hdu 2680 Choose the best route 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不 ...
- HDU 2680 Choose the best route(SPFA)
Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...
- HDU 2680 Choose the best route 最短路问题
题目描述:Kiki想去他的一个朋友家,他的朋友家包括所有的公交站点一共有n 个,一共有m条线路,线路都是单向的,然后Kiki可以在他附近的几个公交站乘车,求最短的路径长度是多少. 解题报告:这道题的特 ...
- HDU 2680 Choose the best route(多起点单终点最短路问题)题解
题意:小A要乘车到s车站,他有w个起始车站可选,问最短时间. 思路:用Floyd超时,Dijkstra遍历,但是也超时.仔细看看你会发现这道题目好像是多源点单终点问题,终点已经确定,那么我们可以直接转 ...
- HDU 2068 Choose the best route
http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...
- hdoj 2680 choose the best route
Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsicknes ...
随机推荐
- PHP+Oracle Instant Client
<?php <b>●Oracleとの接続テスト</b> <hr> <?php // Oracleとの接続 $conn = OCILogon(" ...
- (转)base64编码是怎么工作的?
按:在PHP中级班的课堂上,有位同学问这样一个问题:“我在用 base64_encode 对用户名进行编码时,会出来等号,是不是可以去掉?”跟我来看完这篇文章,答案即揭晓. 1: 为什么需要base6 ...
- js节流函数高级版
节流函数其主要作用就是防止用户在短时间内多次触发该事件. <!DOCTYPE html> <html lang="en"> <head> < ...
- Python 管理 MySQL
Python MySQLdb 模块 Python pymysql 模块 Python SQLAlchemy 模块 Python ConfigParser 模块 Python 创建 MySQL 配置文件 ...
- warning C4305:“初始化”:从“double”到“float”截断
编译VS项目时出现警告: warning C4305:“初始化”:从“double”到“float”截断(warning C4305: 'initializing' : truncation from ...
- spring boot配置文件application.propertis
转自http://www.qiyadeng.com/post/spring-boot-application-properties 本文记录Spring Boot application.proper ...
- struts.xml文件中配置tiles.xml
Apache Tiles是一个JavaEE应用的页面布局框架.Tiles框架提供了一种模板机制,可以为某一类页面定义一个通用的模板,该模板定义了页面的整体布局.布局由可以复用的多个块组成,每个页面可以 ...
- c# linq update单个字段
1.更新单个字段 /// <summary> /// 更新字段 /// </summary> /// <typeparam name="T">& ...
- JS中方法判断存在
function test(){ alert("test"); } if(typeof test!='undefined') {alert(1) test(); } else {a ...
- 如何用微信小程序模仿豆瓣首页
程序思路: 用微信自带组件swiper来实现轮播图 用豆瓣提供的api(这里使用的电影api)来获取最近的电影数据[豆瓣api地址] 获取数据用微信的request方法,只需要提供豆瓣api的url链 ...