最短路之spfa系列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544
输入保证至少存在1条商店到赛场的路线。
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0
2
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std; struct edge {
int v, w;
edge(int v = , int w = ) : v(v), w(w) {}
}; int n, m, a, b, c;
int v[], d[]; vector<edge> G[]; void init() {
for(int i = ; i < ; i++) {
G[i].clear();
}
} void spfa() {
memset(v, , sizeof(v));
memset(d, 0x3f, sizeof(d));
d[] = , v[] = ;
queue<int> q;
q.push();
int x;
while(!q.empty()) {
x = q.front(), q.pop();
v[x] = ;
for(int i = ; i < G[x].size(); i++) {
int y = G[x][i].v, cost = G[x][i].w;
if(d[y] > d[x] + cost) {
d[y] = d[x] + cost;
if(!v[y]) {
q.push(y);
v[y] = ;
}
}
}
}
} int main() {
while(~scanf("%d%d", &n, &m)) {
if(n == && m == )
break;
init();
for(int i = ; i < m; i++) {
scanf("%d%d%d", &a, &b, &c);
G[a].push_back(edge(b, c));
G[b].push_back(edge(a, c));
}
spfa();
printf("%d\n",d[n]);
}
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548
Here comes the problem: when you are on floor A,and you want to go to floor B,how many times at least he has to press the button "UP" or "DOWN"?
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn.
A single 0 indicate the end of the input.
3 3 1 2 5
0
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std; int n, a, b, x;
int v[], d[]; struct edge {
int v, t;
edge(int v = , int t = ) : v(v), t(t) {}
}; vector<edge> G[]; void init() {
for(int i = ; i < ; i++) {
G[i].clear();
}
memset(v, , sizeof(v));
memset(d, 0x3f3f3f3f, sizeof(d));
} void spfa(int s) {
d[s] = , v[s] = ;
queue<int> q;
q.push(s);
int x;
while(!q.empty()) {
x = q.front(), q.pop();
v[x] = ;
for(int i = ; i < G[x].size(); i++) {
int y = G[x][i].v, z = G[x][i].t;
if(d[y] > d[x] + z) {
d[y] = d[x] + z;
if(!v[y]) {
v[y] = ;
q.push(y);
}
}
}
}
} int main() {
while(~scanf("%d", &n) && n) {
init();
scanf("%d%d", &a, &b);
for(int i = ; i <= n; i++) {
scanf("%d", &x);
if(i + x <= n) {
G[i].push_back(edge(i + x, ));
//G[i+x].push_back(edge(i,1));
}
if(i - x >= ) {
G[i].push_back(edge(i - x, ));
//G[i-x].push_back(edge(i, 1));
}
}
spfa(a);
printf("%d\n", d[b] >= 0x3f3f3f3f ? - : d[b]);
}
return ;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790
(1<n<=1000, 0<m<100000, s != t)
1 2 5 6
2 3 4 5
1 3
0 0
代码实现如下:
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std; struct edge {
int v, l, cost;
edge(int v = , int l = , int cost = ) : v(v), l(l), cost(cost) {}
}; int n, m, a, b, e, f, s, t;
int v[], d[], c[]; vector<edge> G[]; void init() {
for(int i = ; i < ; i++) {
G[i].clear();
}
memset(v, , sizeof(v));
memset(d, 0x3f, sizeof(d));
memset(c, 0x3f, sizeof(c));
} void spfa(int s) {
d[s] = , v[s] = , c[s] = ;
queue<int> q;
q.push(s);
int x;
while(!q.empty()) {
x = q.front(), q.pop();
v[x] = ;
for(int i = ; i < G[x].size(); i++) {
int y = G[x][i].v, p = G[x][i].l, val = G[x][i].cost;
if(d[y] >= d[x] + p) {
if(d[y] == d[x] + p) {
c[y] = min(c[y], c[x] + val);
} else {
c[y] = c[x] + val;
}
d[y] = d[x] + p;
if(!v[y]) {
q.push(y);
v[y] = ;
}
}
}
}
} int main() {
while(~scanf("%d%d", &n, &m)) {
if(n == && m == )
break;
init();
for(int i = ; i < m; i++) {
scanf("%d%d%d%d", &a, &b, &e, &f);
G[a].push_back(edge(b, e, f));
G[b].push_back(edge(a, e, f));
}
scanf("%d%d", &s, &t);
spfa(s);
printf("%d %d\n", d[t], c[t]);
}
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2066
接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车程是time小时;(1=<(a,b)<=1000;a,b 之间可能有多条路)
接着的第T+1行有S个数,表示和草儿家相连的城市;
接着的第T+2行有D个数,表示草儿想去地方。
1 3 5
1 4 7
2 8 12
3 8 4
4 9 12
9 10 2
1 2
8 9 10
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = ; int T, n, m, a, b, tt;
int v[maxn], d[maxn], mp[maxn][maxn]; vector<int> G[maxn]; void init() {
for(int i = ; i < maxn; i++) {
G[i].clear();
}
memset(v, , sizeof(v));
memset(d, 0x3f3f3f3f, sizeof(d));
memset(mp, 0x3f3f3f3f, sizeof(mp));
} void spfa(int s) {
v[s] = , d[s] = ;
queue<int> q;
q.push(s);
int x;
while(!q.empty()) {
x = q.front(), q.pop();
v[x] = ;
for(int i = ; i < G[x].size(); i++) {
int y = G[x][i], z = mp[x][y];
if(d[y] > d[x] + z) {
d[y] = d[x] + z;
if(!v[y]) {
q.push(y);
v[y] = ;
}
}
}
}
} int main() {
while(~scanf("%d%d%d", &T, &n, &m)) {
init();
for(int i = ; i < T; i++) {
scanf("%d%d%d", &a, &b, &tt);
G[a].push_back(b);
G[b].push_back(a);
mp[a][b] = min(mp[a][b], tt);
mp[b][a] = min(mp[b][a], tt);
}
for(int i = ; i < n; i++) {
scanf("%d", &a);
spfa(a);
}
int mm = 0x3f3f3f3f;
for(int i = ; i < m; i++) {
scanf("%d", &a);
mm = min(mm, d[a]);
}
printf("%d\n", mm);
}
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112
这样住了一段时间,徐总对当地的交通还是不太了解。有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格)。
徐总经常会问蹩脚的英文问路:“Can you help me?”。看着他那迷茫而又无助的眼神,热心的你能帮帮他吗?
请帮助他用最短的时间到达目的地(假设每一路公交车都只在起点站和终点站停,而且随时都会开)。
第二行有徐总的所在地start,他的目的地end;
接着有n行,每行有站名s,站名e,以及从s到e的时间整数t(0<t<100)(每个地名是一个长度不超过30的字符串)。
note:一组数据中地名数不会超过150个。
如果N==-1,表示输入结束。
xiasha westlake
xiasha station 60
xiasha ShoppingCenterofHangZhou 30
station westlake 20
ShoppingCenterofHangZhou supermarket 10
xiasha supermarket 50
supermarket westlake 10
-1
Hint:
The best route is:
xiasha->ShoppingCenterofHangZhou->supermarket->westlake
虽然偶尔会迷路,但是因为有了你的帮助
**和**从此还是过上了幸福的生活。
――全剧终――
#include <map>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; int n, num, cost;
string s1, s2;
int v[], d[]; struct edge {
int v, t;
edge(int v = , int t = ) : v(v), t(t) {}
}; vector<edge> G[]; void init() {
memset(v, , sizeof(v));
memset(d, 0x3f3f3f3f, sizeof(d));
for(int i = ; i < ; i++) {
G[i].clear();
}
} void spfa(int s) {
d[s] = , v[s] = ;
queue<int> q;
q.push(s);
int x;
while(!q.empty()) {
x = q.front(), q.pop();
v[x] = ;
for(int i = ; i < G[x].size(); i++) {
int y = G[x][i].v, z = G[x][i].t;
if(d[y] > d[x] + z) {
d[y] = d[x] + z;
if(!v[y]) {
q.push(y);
v[y] = ;
}
}
}
}
} int main() {
while(~scanf("%d", &n) && (n != -)) {
cin >>s1 >>s2;
num = ;
init();
map<string, int> id;
id[s1] = num++;
id[s2] = num++;
int s = id[s1], e = id[s2];
for(int i = ; i < n; i++) {
cin >>s1 >> s2 >>cost;
if(!id.count(s1)) {
id[s1] = num++;
}
if(!id.count(s2)) {
id[s2] = num++;
}
G[id[s1]].push_back(edge(id[s2], cost));
G[id[s2]].push_back(edge(id[s1], cost));
}
spfa(s);
printf("%d\n", d[e] >= 0x3f3f3f3f ? - : d[e]);
}
}
最短路之spfa系列的更多相关文章
- 模板C++ 03图论算法  1最短路之单源最短路(SPFA)
		3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ... 
- ACM/ICPC 之 最短路-Floyd+SPFA(BFS)+DP(ZOJ1232)
		这是一道非常好的题目,融合了很多知识点. ZOJ1232-Adventrue of Super Mario 这一题折磨我挺长时间的,不过最后做出来非常开心啊,哇咔咔咔 题意就不累述了,注释有写,难点在 ... 
- POJ 2449Remmarguts' Date K短路模板 SPFA+A*
		K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ... 
- ACM-最短路(SPFA,Dijkstra,Floyd)之最短路——hdu2544
		***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ... 
- BZOJ_1614_ [Usaco2007_Jan]_Telephone_Lines_架设电话线_(二分+最短路_Dijkstra/Spfa)
		描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1614 分析 类似POJ_3662_Telephone_Lines_(二分+最短路) Dijks ... 
- 洛谷最短路计数SPFA
		题目描述 给出一个N个顶点M条边的无向无权图,顶点编号为1-N.问从顶点1开始,到其他每个点的最短路有几条. 输入输出格式 输入格式: 输入第一行包含2个正整数N,M,为图的顶点数与边数. 接下来M行 ... 
- UESTC30-最短路-Floyd最短路、spfa+链式前向星建图
		最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同 ... 
- 最短路(spfa)
		http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others) Memory ... 
- 2018/1/28 每日一学 单源最短路的SPFA算法以及其他三大最短路算法比较总结
		刚刚AC的pj普及组第四题就是一种单源最短路. 我们知道当一个图存在负权边时像Dijkstra等算法便无法实现: 而Bellman-Ford算法的复杂度又过高O(V*E),SPFA算法便派上用场了. ... 
随机推荐
- Windows SDK 非模态对话框的消息处理
			在SDK中使用非模态对话框时的几个问题: 1.为什么要调用IsDialogMessage?? 2.非模态对话框与主窗口有什么区别? 3.如果不调用IsDialogMessage,消息能不能传递到对话框 ... 
- 【Docker 命令】- pause/unpause 命令
			docker pause :暂停容器中所有的进程. docker unpause:恢复容器中所有的进程. 语法 docker pause [OPTIONS] CONTAINER [CONTAINER. ... 
- (转)Linux NUMA引发的性能问题
			最近某客户的核心业务系统又出了翻译缓慢的情况.该问题在6月份也出现过,当时进行了一次调整. 我们首先来看下故障时间段的awr报告: 单纯的从TOP 5 event,基本上是看不出任何东西的,可能有人会 ... 
- python redis插件安装
			#tar xvzf redis-py-2.2.1.tar.gz #cd redis-py-2.2.1 #python setup.py install 附件: https://app.yinxia ... 
- Android 布局方式学习
			一.LinearLayout线性布局: 线性布局是程序中最常见的一种布局方式,线性布局可以分为水平线性布局和垂直线性布局两种, 通过android:orientation属性可以设置线性布局的方向 1 ... 
- [NOI2009]诗人小G  决策单调性优化DP
			第一次写这种二分来优化决策单调性的问题.... 调了好久,,,各种细节问题 显然有DP方程: $f[i]=min(f[j] + qpow(abs(sum[i] - sum[j] - L - 1))); ... 
- 洛谷3258:[USACO2012 MAR]Flowerpot 花盆——题解
			https://www.luogu.org/problemnew/show/P2698#sub 老板需要你帮忙浇花.给出N滴水的坐标,y表示水滴的高度,x表示它下落到x轴的位置. 每滴水以每秒1个单位 ... 
- 51nod 1962 区间计数(单调栈+二分)
			维护两个单调递减的栈,当i加进栈,位置x的数弹出的时候,在另一个栈中找到和这个数一样大的数,计算贡献(x-靠右左端点)*(i-x). #include<iostream> #include ... 
- 【BZOJ 4455】 [Zjoi2016]小星星 容斥计数
			dalao教导我们,看到计数想容斥……卡常策略:枚举顺序.除去无效状态.(树结构) #include <cstdio> #include <cstring> #include ... 
- nfs挂载权限问题
			问题: 服务器A:192.168.10.230 服务器B:192.168.10.231 由于服务器A空间不足,打算将服务器A产生的数据库日志挂载到服务器B上,刚开始设定的anonuid和anongid ... 
