HDU 2680 Choose the best route(多起点单终点最短路问题)题解
题意:小A要乘车到s车站,他有w个起始车站可选,问最短时间。
思路:用Floyd超时,Dijkstra遍历,但是也超时。仔细看看你会发现这道题目好像是多源点单终点问题,终点已经确定,那么我们可以直接转置邻接矩阵,从终点找最小的起点,转换成了单源最短路问题。
代码:
#include<cstdio>
#include<set>
#include<cmath>
#include<stack>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = +;
const int INF = 0x3f3f3f3f;
int mp[maxn][maxn];
int dis[maxn];
int vis[maxn];
int n,m;
void dijkstra(int st){
memset(vis,,sizeof(vis));
memset(dis,INF,sizeof(dis));
dis[st] = ;
for(int i = ;i <= n;i++){
int Min = INF,k = ;
for(int j = ;j <= n;j++){
if(!vis[j] && dis[j] < Min){
Min = dis[j];
k = j;
}
}
vis[k] = ;
for(int j = ;j <= n;j++){
if(dis[j] > dis[k] + mp[k][j]){
dis[j] = dis[k] + mp[k][j];
}
}
}
} int main(){
int s;
while(scanf("%d%d%d",&n,&m,&s) != EOF){
memset(mp,INF,sizeof(mp));
while(m--){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
mp[v][u] = min(mp[v][u],w);
}
dijkstra(s);
int ans = INF;
scanf("%d",&m);
while(m--){
int u;
scanf("%d",&u);
ans = min(ans,dis[u]);
}
if(ans == INF) printf("-1\n");
else printf("%d\n",ans);
}
return ;
}
HDU 2680 Choose the best route(多起点单终点最短路问题)题解的更多相关文章
- 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 最短路问题
题目描述:Kiki想去他的一个朋友家,他的朋友家包括所有的公交站点一共有n 个,一共有m条线路,线路都是单向的,然后Kiki可以在他附近的几个公交站乘车,求最短的路径长度是多少. 解题报告:这道题的特 ...
- 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 (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 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 ...
- HDU 2612 Find a way【多起点多终点BFS/两次BFS】
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
随机推荐
- 【Java nio】Channel
package com.slp.nio; import org.junit.Test; import java.io.*; import java.nio.ByteBuffer; import jav ...
- 使用HttpClient以文件流的方式上传文件(非multipartFormData方式)
@Test public void testAdd() throws IOException { HttpPost post = new HttpPost("http://localhost ...
- intellij IDEA 报 非法字符 \65279 原因及解决方法
用eclipse创建的项目导入到 intellij IDEA 之后 编译时包 非法字符 '\65279' 该问题产生的原因是 IDEA对以UTF8编码的文件保存时自动加上了BOM(UTF-8文件签名) ...
- node中的对象
1. class的概念 定义一个class,属性都是private,方法都是public. Hello.js: 使用class index.js: 2. 单例类 使用exports而不是module. ...
- 【BZOJ1176】[Balkan2007]Mokia/【BZOJ2683】简单题 cdq分治
[BZOJ1176][Balkan2007]Mokia Description 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=1600 ...
- @Apiimplicitparam的paramType
转自:https://swagger.io/docs/specification/describing-parameters/ Parameter Types OpenAPI 3.0 distingu ...
- Linux系统下 Rsync 环境安装搭建
一.Rsync简介 1.认识 Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本地和远 ...
- [Android Tips] 32. 解决 Android Device Monitor 在 Mac OS X 卡住
Alternatively you can keep latest jdk and update swt used by monitor: get updated swt: https://www.e ...
- Python大数据:外部数据获取(网页抓取)
import urllib2 as url import cookielib,StringIO,gzip,json import pandas as pd import numpy as np #定义 ...
- 二.re库介绍
一.re库的主要功能函数 1.re.search()用法 2.re.match()的用法 >>> match=re.match(r'[1-9]\d{5}','BIT 100081') ...