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会默认显示一条结果.但同一个城市可能有多个机场和火车站,那么,如何用 ...
随机推荐
- ubuntu配置lua环境,并进行c与lua的相互调用
1.安装lua环境 先查看一下apt可获取的lua版本 我们选择lua5.1版本进行安装 sudo apt install lua5.1 安装完之后测试一下是否安装成功,如果可以正常使用,则lua环境 ...
- Ubuntu 16.04下安装zsh和oh-my-zsh
注意:安装前先备份/etc/passwd 一开始装oh-my-zsh我是拒绝的,因为这东西安装容易,卸载难,真的很难. Mac安装参考:http://www.cnblogs.com/EasonJim/ ...
- Keil5下载STM32库
1.http://www.keil.com/dd2 2.3.以STM32L051C8为例 下载即可.
- C# this调用构造函数及析构函数
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace trai ...
- JavaScript之不规则Table转化为可定点索引td节点的网格矩阵【插件】
由于解析课程表的缘故,有如下需求: 1. 将任意表格解析成独立的单元格矩阵[本次博文的缘由] 2. 根据矩阵坐标,确定任意一格的节点 /* 表格-->网格化 标记表格的位置及其对应的节点 * ...
- JavaScript之函数式编程思想初探
//result = 3*x + 5; var Mul3 = function(x){ return 3*x; } var Add5 = function(x){ return x + 5; } va ...
- JavaScript之小工具之日志log()[兼容]
function log(){ try{ console.log.apply(console,arguments); }catch(e){ try{ opera.postError.apply(ope ...
- node之常用模块
http express cheerio superagent url events fs util querystring request
- HDU 2522 A simple problem (模拟)
题目链接 Problem Description Zty很痴迷数学问题..一天,yifenfei出了个数学题想难倒他,让他回答1 / n.但Zty却回答不了^_^. 请大家编程帮助他. Input 第 ...
- TCP传输协议
TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接,四次挥手断开连接. 三次握手 是指建立一个TCP连接时,需要客户端和服务端总共发送3个包以确认连接建立成功.在so ...