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会默认显示一条结果.但同一个城市可能有多个机场和火车站,那么,如何用 ...
随机推荐
- bootstrap3 input 验证样式【转】
feedback <form role="form"> <div class="form-group has-success has-feedback& ...
- CSS进阶之模拟Bootstrap网格布局
目前暂时实现效果,容后面整理心得,先贴上源代码. 源码 <!DOCTYPE html> <html> <head> <title>demo bootst ...
- JS 中对变量类型判断的几种方式
文章整理搬运,出处不详,如有侵犯,请联系~ 数据类型判断和数据类型转换代码工具 在 JS 中,有 5 种基本数据类型和 1 种复杂数据类型,基本数据类型有:Undefined, Null, Boo ...
- jQuery - 几种异步提交方式
$.post(url,params,callback); $.post("${ctx}/role/grant", {userId : $("#userId"). ...
- http://blog.csdn.net/w_e_i_/article/details/70766035
http://blog.csdn.net/w_e_i_/article/details/70766035
- Shiro后台实现验证权限
今天发现一个问题:使用shiro的时候,虽然隐藏掉了一些菜单,但是当我们通过get请求直接访问菜单的时候还是会访问到,也就是shiro可以在界面实现隐藏一些信息,但是没有真正的根据权限码验证请求,于是 ...
- Junit的Assert用法
package junit.framework; /** * A set of assert methods. Messages are only displayed when an assert f ...
- nginx入门二
反向代理: proxy_pass server { listen 80; location /n { proxy_pass http://127.0.0.1:8000/test; } location ...
- setfacl报错Operation not supported
对文件目录setfacl权限设置时报错Operation not supported Google一下,发现是分区acl权限问题 一般情况下(ext4),默认acl支持都是加载的.但如果遇到二般情况, ...
- ES系列十六、集群配置和维护管理
一.修改配置文件 1.节点配置 1.vim elasticsearch.yml # ======================== Elasticsearch Configuration ===== ...