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会默认显示一条结果.但同一个城市可能有多个机场和火车站,那么,如何用 ...
随机推荐
- JAVA记录-redis缓存机制介绍(四)
Redis 数据备份与恢复 Redis SAVE 命令用于创建当前数据库的备份. 语法 redis Save 命令基本语法如下: redis 127.0.0.1:6379> SAVE 实例 re ...
- Prim算法:最小生成树
#define _CRT_SECURE_NO_WARNINGS /* 7 10 0 1 5 0 2 2 1 2 4 1 3 2 2 3 6 2 4 10 3 5 1 4 5 3 4 6 5 5 6 9 ...
- 用贪心算法近似求解 Loading Balance 问题(作业调度的负载均衡)
一,Loading Balance 问题描述:有 m 台相同的机器及 n 个作业,其中 m={M(1),M(2),……M(m)}.n = {J(1),J(2),……J(n)}.每个作业都有一个处理时间 ...
- FastReport"Text"对象中的HTML标签介绍以及使用
"Text"对象可以理解一些简单的HTML标签.标签可位于对象中的文本.在默认情况下,标签是禁用的,要启用这些HTML标签,可选择对象上下文菜单中的“Allow HTML tags ...
- asp.net 使用一般处理程序和ajax post实现登录以及记住密码
1.登录页面login.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind=&qu ...
- 顺序列表(栈/队列等)ADT[C++]
#include<iostream> using namespace std; //ADT template<class T> class SeqList{ public: / ...
- ABAP知识点笔记
1,获取光标所在行 READ TABLE TD_ALV_DATA INTO TH_ALV_DATA INDEX RS_SELFIELD-TABINDEX. 2,获取alv可编辑单元格内容 DATA: ...
- mysql 案例~select引起的性能问题
案例1 背景:测试环境下发现大量select查询,而且负载飙升到90+ 排查思路: 1 老规则,按照排错脚本走一圈,规划出几个元素(1 针对库访问的统计 2针对具体语句类型的统计),发现有大量的sel ...
- 北洋UAM-05LX(网口系列适用)ROS节点
参考创客智造ROS与激光雷达入门教程 说明: 介绍ROS如何接入Hokuyo网口的雷达及基本使用 测试雷达:UAM-05LX采用太网接口,如果型号是USB口的参考教程 ros wiki: http:/ ...
- ubuntu下好用的音乐播放器audacious
audacious是ubuntu下一款非常好用的音乐播放器,万能的音乐播放器而且简洁美观,可以播放ape各种无损发烧音乐格式. 如果想听音乐的话,现在百度音乐,酷我音乐,酷狗音乐等都是有网络播放器的, ...