hdu 2066 多起点 多终点
多起点 多终点 无向图 结点的个数要自己求
Sample Input
6 2 3 //边数 起点数 终点数
1 3 5 //u v w
1 4 7
2 8 12
3 8 4
4 9 12
9 10 2
1 2 //起点
8 9 10 //终点
Sample Output
9
# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ; const int MAXN=;
const int INF=0x3f3f3f3f;
int n ;
bool vis[MAXN];
int cost[MAXN][MAXN] ;
int lowcost[MAXN] ;
int pre[MAXN];
void Dijkstra(int beg)
{
for(int i=;i<n;i++)
{
lowcost[i]=INF;vis[i]=false;pre[i]=-;
}
lowcost[beg]=;
for(int j=;j<n;j++)
{
int k=-;
int Min=INF;
for(int i=;i<n;i++)
if(!vis[i]&&lowcost[i]<Min)
{
Min=lowcost[i];
k=i;
}
if(k==-)break;
vis[k]=true;
for(int i=;i<n;i++)
if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i])
{
lowcost[i]=lowcost[k]+cost[k][i];
pre[i]=k;
}
}
} int main ()
{
//freopen("in.txt","r",stdin) ;
int m , s , e;
while (scanf("%d %d %d" , &m , &s , &e) !=EOF)
{
int u , v , w ;
int i , j , t = - ;
int a[MAXN] ;
int b[MAXN] ;
memset(cost, INF, sizeof(cost));
while(m--)
{
scanf("%d %d %d" , &u , &v , &w) ;
if (w < cost[u-][v-] )
{
cost[u-][v-] = w ;
cost[v-][u-] = w ;
} if (u > t)
t = u ;
if (v > t)
t = v ;
}
n = t ;
for (i = ; i < s ; i++)
scanf("%d" , &a[i]) ;
for (i = ; i < e ; i++)
scanf("%d" , &b[i]) ;
int ans = INF ;
for (i = ; i < s ; i++)
{
Dijkstra(a[i]-) ;
for (j = ; j < e ; j++)
{
if (lowcost[b[j]-] < ans)
ans = lowcost[b[j]-] ;
} }
printf("%d\n" , ans) ;
} return ;
}
堆优化
# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# include <queue>
# define LL long long
using namespace std ; const int INF=0x3f3f3f3f;
const int MAXN=;
struct qnode
{
int v;
int c;
qnode(int _v=,int _c=):v(_v),c(_c){}
bool operator <(const qnode &r)const
{
return c>r.c;
}
};
struct Edge
{
int v,cost;
Edge(int _v=,int _cost=):v(_v),cost(_cost){}
};
vector<Edge>E[MAXN];
bool vis[MAXN];
int dist[MAXN];
int n ;
void Dijkstra(int start)//点的编号从1开始
{
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++)dist[i]=INF;
priority_queue<qnode>que;
while(!que.empty())que.pop();
dist[start]=;
que.push(qnode(start,));
qnode tmp;
while(!que.empty())
{
tmp=que.top();
que.pop();
int u=tmp.v;
if(vis[u])continue;
vis[u]=true;
for(int i=;i<E[u].size();i++)
{
int v=E[tmp.v][i].v;
int cost=E[u][i].cost;
if(!vis[v]&&dist[v]>dist[u]+cost)
{
dist[v]=dist[u]+cost;
que.push(qnode(v,dist[v]));
}
}
}
}
void addedge(int u,int v,int w)
{
E[u].push_back(Edge(v,w));
} int main ()
{
// freopen("in.txt","r",stdin) ;
int m , s , e;
while (scanf("%d %d %d" , &m , &s , &e) !=EOF)
{ int u , v , w ;
int i , j ;
int a[MAXN] ;
int b[MAXN] ;
for(i=;i<=MAXN;i++)
E[i].clear();
n = ;
while(m--)
{
scanf("%d%d%d" , &u , &v , &w) ;
addedge(u,v,w) ;
addedge(v,u,w) ;
n = max(n,max(u,v)) ;
}
for (i = ; i < s ; i++)
scanf("%d" , &a[i]) ;
for (i = ; i < e ; i++)
scanf("%d" , &b[i]) ;
int ans = INF ;
for (i = ; i < s ; i++)
{
Dijkstra(a[i]) ;
for (j = ; j < e ; j++)
{
if (dist[b[j]] < ans)
ans = dist[b[j]] ;
} }
printf("%d\n" , ans) ; } return ;
}
hdu 2066 多起点 多终点的更多相关文章
- hdu 2680 多起点一终点
注意这是一个有向图! 多起点,一终点 反过来,看成一个起点,多个终点,找最短路 因为是有向图 所以u->v 要也要反过来成为v->u Sample Input5 8 5 //结点数 边数 ...
- # H - H HDU - 2066 (多起点、多终点问题)
H - H HDU - 2066 (多源点.多汇点问题) 一个图上,有M条边,Z个出发点,Y个终止点.求一条最短路,其中起点是Z中的任意一点,终点是Y中任意一点. Input 输入数据有多组,输入直到 ...
- Key Vertex (hdu 3313 SPFA+DFS 求起点到终点路径上的割点)
Key Vertex Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- HDU——2612Find a way(多起点多终点BFS)
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU2066一个人的旅行---(多起点多终点最短路径)
http://acm.hdu.edu.cn/showproblem.php?pid=2066 一个人的旅行 Time Limit: 1000/1000 MS (Java/Others) Memo ...
- HDU 2066 最短路floyd算法+优化
http://acm.hdu.edu.cn/showproblem.php?pid=206 题意 从任意一个邻居家出发 到达任意一个终点的 最小距离 解析 求多源最短路 我想到的是Floyd算法 但是 ...
- HDU 2066 一个人的旅行【Dijkstra 】
题意:给出s个起点,d个终点,问从这些起点到达终点的最短距离 因为有多个起点,所以把这多个起点的值设为0 哎= =改了好久的说= = 是因为在代码里面的t,不知道为什么调用dijkstra()函数之后 ...
- dump 验证实例恢复的起点和终点
什么时候会产生实例恢复呢?当你数据库服务器异常断电,重启数据库就会发生实例恢复.实例恢复是由数据库自动完成的,无须DBA的干涉.当然这里有个前提条件:数据文件. 在线日志文件.控制文件不得有损坏. 我 ...
- 【百度地图API】让用户选择起点和终点的驾车导航
原文:[百度地图API]让用户选择起点和终点的驾车导航 摘要: 如果用户搜索“从机场到火车站”,使用驾车导航DrivingRoute会默认显示一条结果.但同一个城市可能有多个机场和火车站,那么,如何用 ...
随机推荐
- Python 爬虫: 抓取花瓣网图片
接触Python也好长时间了,一直没什么机会使用,没有机会那就自己创造机会!呐,就先从爬虫开始吧,抓点美女图片下来. 废话不多说了,讲讲我是怎么做的. 1. 分析网站 想要下载图片,只要知道图片的地址 ...
- Gym - 100085G - GCD Guessing Game
原题链接 题意一个数字x在1-n之间,现在猜数字,每次猜一个数字a,告知gcd(x, a)的答案,问最坏情况下需要猜几次 分析 考虑素数.当猜的数为一组素数的乘积时,就可以把这些素数都猜出来.那么答案 ...
- ThinkPHP 3.2 支付宝即时到账接口开发
前言: 一.支付流程 构造请求参数 向支付宝网关发送请求 生成支付宝页面 支付宝交易结果 二.构建支付类 1.官方即时到账文档地址: https://doc.open.alipay.com/doc2/ ...
- Django 2.0.1 官方文档翻译:编写你的第一个djang补丁(page 15)
编写你的第一个djang补丁(page 15) 介绍 有兴趣为社区做一些贡献?可能你发现了django中的一个你想修复的bug,或者你你想添加一个小小的功能. 回馈django就是解决你遇到的问题的最 ...
- dubbo集群服务下一台服务挂了对服务调用的影响
一.问题描述:项目中2台dubbo服务给移动端提供查询接口,移动端反应说查询时而很快(秒刷),时而很慢(4-5秒). 二.问题分析: 1.问题猜想:网络不稳定原因导致,但是切换公司wifi和手机4G, ...
- MySQL中查询行数最多的表并且排序
#切换到schema use information_schema; #查询数据量最大的30张表 并排序 select table_name,table_rows from tables order ...
- python队列queue 之优先级队列
import queue as Q def PriorityQueue_int(): que = Q.PriorityQueue() que.put(10) que.put(1) que.put(5) ...
- 2、SpringBoot接口Http协议开发实战8节课(7-8)
7.SpringBoot2.x文件上传实战 简介:讲解HTML页面文件上传和后端处理实战 1.讲解springboot文件上传 MultipartFile file,源自SpringMVC 1)静态页 ...
- Gaussian discriminant analysis 高斯判别分析
高斯判别分析(附Matlab实现) 生成学习算法 高斯判别分析(Gaussian Discriminant analysis,GDA),与之前的线性回归和Logistic回归从方法上讲有很大的不同,G ...
- SpringMVC使用Redis作为缓存提供者
(1)pom添加依赖项 <dependency> <groupId>org.springframework.data</groupId> <artifactI ...