基础最短路(模板 spfa)
Description
Input
接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车程是time小时;(1=<(a,b)<=1000;a,b 之间可能有多条路)
接着的第T+1行有S个数,表示和草儿家相连的城市;
接着的第T+2行有D个数,表示草儿想去地方。
Output
Sample Input
Sample Output
# include<iostream>
# include<cstdio>
# include<cstring>
# include<queue>
# include<algorithm>
using namespace std;
const int INF=<<;
struct edge
{
int to,w,nxt;
};
edge e[];
int head[],dis[];
int t,s,d,cnt,sta[],ed[];
void add(int a,int b,int c)
{
e[cnt].to=b;
e[cnt].w=c;
e[cnt].nxt=head[a];
head[a]=cnt++;
}
int spfa(int from,int to)
{
fill(dis,dis+,INF);
dis[from]=;
queue<int>q;
q.push(from);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=e[i].nxt){
if(dis[e[i].to]>dis[u]+e[i].w){
dis[e[i].to]=dis[u]+e[i].w;
q.push(e[i].to);
}
}
}
return dis[to];
}
int main()
{
int i,j,a,b,c;
while(scanf("%d%d%d",&t,&s,&d)!=EOF)
{
cnt=;
memset(head,-,sizeof(head));
for(i=;i<=t;++i){
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
for(i=;i<s;++i)
scanf("%d",&sta[i]);
for(i=;i<d;++i)
scanf("%d",&ed[i]);
int ans=INF;
for(i=;i<s;++i)
for(j=;j<d;++j)
ans=min(ans,spfa(sta[i],ed[j]));
printf("%d\n",ans);
}
return ;
}
基础最短路(模板 spfa)的更多相关文章
- POJ 2449Remmarguts' Date K短路模板 SPFA+A*
K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ...
- 最短路模板[spfa][dijkstra+堆优化][floyd]
借bzoj1624练了一下模板(虽然正解只是floyd) spfa: #include <cstdio> #include <cstring> #include <alg ...
- 最短路模板(SPFA POJ2387)
#include <set> #include <map> #include <queue> #include <stack> #include < ...
- 最短路模板|堆优化Dijkstra,SPFA,floyd
Ⅰ:Dijkstra单源点最短路 1.1Dijkstra const int MAX_N = 10000; const int MAX_M = 100000; const int inf = 0x3f ...
- 模板C++ 03图论算法 1最短路之单源最短路(SPFA)
3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...
- poj1511/zoj2008 Invitation Cards(最短路模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Invitation Cards Time Limit: 5 Seconds ...
- [poj2449]Remmarguts' Date(K短路模板题,A*算法)
解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...
- poj 2499第K短路模板
第k*短路模板(单项边) #include <iostream> #include <cstdio> #include <algorithm> #include & ...
- K短路模板POJ 2449 Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536K Total Submissions:32863 Accepted: 8953 Description &qu ...
- POJ 2387 Til the Cows Come Home(最短路模板)
题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...
随机推荐
- CSS3实现8种Loading效果【二】
CSS3实现8种Loading效果[二] 今晚吃完饭回宿舍又捣鼓了另外几种Loading效果,老规矩,直接“上菜“…… 注:gif图片动画有些卡顿,非实际效果! 第一种效果: 代码如下: < ...
- C++微专业课程辅导(内存模型和动态内存)
“除了静态内存和栈内存之外,每个程序还拥有一个内存池.这部分空间被称作自由空间(free store)或堆(heap).程序用堆来存储动态分配(dynamically allocate)的对象”——& ...
- java_test_week4
20165310 week4 JDK知识点 启动JDK: javac -g <java>:参数一定要加上-g jdk -classpath .:./bin <class>:一开 ...
- Python之GUI的最终选择(Tkinter)
首先,Tkinter是Python默认的GUI库,想IDLE就是用Tkinter设计出来的,因此直接导入Tkinter模块就可以啦 1 import tkinter (1)Tkinter初体验: 1 ...
- goole机器学习视频链接【学习笔记】
作者:庄泽彬 说明:在youtu上观看的google的机器学习相关的视频,如何fangqiang请自己解决 机器学习简介:https://www.youtube.com/watch?time_cont ...
- 如何创建自己的python包
写过python的人都知道python最方便也最牛的地方就是它有无数的第三方lib可以直接拿来使用,可以让编写代码变的更容易. 长用的安装第三方lib的方法有easy_install和pip,这两个的 ...
- POJ 3480 John(SJ定理博弈)题解
题意:n堆石头,拿走最后一块的输 思路:SJ定理:先手必胜当且仅当:(1)游戏的SG函数不为0且游戏中某个单一游戏的SG函数大于1:(2)游戏的SG函数为0且游戏中没有单一游戏的SG函数大于1. 参考 ...
- 《Robot Framework自动化测试修炼宝典》道长
1. Python下载https://www.python.org/downloads 2. Setuptools下载https://pypi.python.org/pypi/setuptools用原 ...
- Qt加载OSg视图例子
//QT += core gui opengl //LIBS += -losgViewer -losgDB -losgUtil -losg -lOpenThreads -losgGA -losgQt ...
- spring boot 开发 org.springframework.context.ApplicationContextException: Unable to start web server;
Error starting ApplicationContext. To display the conditions report re-run your application with 'de ...