POJ 2387 Til the Cows Come Home BFS最短路求解
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=66569#problem/A
题意:给定某些点之间的距离,起终点一定,求回到目的地的最短距离。
以前用的是Djikstra写的,矩阵的最短路写多了,第一想到的就是BFS,Dijkstra的循环写法似乎有点忘记了。习惯性的还是比较喜欢用邻接矩阵来存储,练习下邻接表的写法。
邻接矩阵:
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<string>
#define LL long long
#define INF 0x7fffffffusing namespace std;int Map[1100][1100],dis[1100],vis[1100];
int T,N;void bfs(){queue<int> Q;memset(dis,-1,sizeof(dis));memset(vis,0,sizeof(vis));Q.push(N);vis[N]=1;dis[N]=0;while(!Q.empty()){int tp=Q.front();Q.pop();vis[tp]=0;for(int i=1;i<=N;i++){if(Map[tp][i]==-1) continue;if(dis[i]!=-1 && dis[tp]+Map[tp][i]>=dis[i]) continue;dis[i]=dis[tp]+Map[tp][i];if(!vis[i]) Q.push(i),vis[i]=1;}}cout<<dis[1]<<endl;
}int main(){//freopen("D:\\in.txt","r",stdin);while(cin>>T){cin>>N;int a,b,c;memset(Map,-1,sizeof(Map));for(int i=0;i<T;i++){cin>>a>>b>>c;if(Map[a][b]==-1) Map[a][b]=Map[b][a]=c;else Map[a][b]=Map[b][a]=min(Map[a][b],c);}bfs();}return 0;
}
邻接表:
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<string>
#define LL long long
#define INF 0x7fffffffusing namespace std;struct edge{int u,v,w;
};int N,T,dis[1100],vis[1100];
vector<int> G[1100];
vector<edge> E;void bfs(){memset(dis,-1,sizeof(dis));memset(vis,0,sizeof(vis));queue<int> Q;Q.push(N);vis[N]=1;dis[N]=0;while(!Q.empty()){int tp=Q.front();Q.pop();vis[tp]=0;for(int p=0;p<(int)G[tp].size();p++){edge &e=E[G[tp][p]];int u=e.u,v=e.v,w=e.w;if(dis[v]!=-1 && dis[u]+w>=dis[v]) continue;dis[v]=dis[u]+w;if(!vis[v]) Q.push(v),vis[v]=1;}}cout<<dis[1]<<endl;
}int main(){//freopen("D:\\in.txt","r",stdin);while(cin>>T){cin>>N;for(int i=0;i<=N;i++) G[i].clear();E.clear();int a,b,c;for(int i=0;i<T;i++){cin>>a>>b>>c;E.push_back((edge){a,b,c});G[a].push_back((int)E.size()-1);E.push_back((edge){b,a,c});G[b].push_back((int)E.size()-1);}bfs();}return 0;
}
POJ 2387 Til the Cows Come Home BFS最短路求解相关推荐
- POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)
原题链接:Til the Cows Come Home 题目大意:有 个点,给出从 点到 点的距离并且 和 是互相可以抵达的,问从 到 的最短距离. 题目分析:这是一道典型的最短路径模版 ...
- Poj 2387 Til the Cows Come Home 迪杰斯特拉(普通+优化)
Til the Cows Come Home 迪杰斯特拉(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回来. 农场主约翰的田里有n( ...
- POJ 2387 Til the Cows Come Home
传送门:http://poj.org/problem?id=2387 这是最短路径问题,本题有重边,但是spfa能解决这个问题: 实现代码: SPFA: 1 #include <iostream ...
- POJ - 2387 Til the Cows Come Home
感觉一直写的dij堆优化都是假的,最短路板子spfa+ dij堆优化 SPFA #include<stdio.h> #include<string.h> #include< ...
- poj 2387 Til the Cows Come Home dijkstra
题意: 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回来. 农场主约翰的田里有n(2<=n<=1000)个地标,唯一编号为1-n.地标 ...
- Til the Cows Come Home(最短路-Dijkstra)
Til the Cows Come Home(最短路-Dijkstra) judge:https://vjudge.net/contest/297882#problem/A Time limit:10 ...
- 【POJ】2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 题意:求从1到n的最短路 题解:板子题.spfa. 代码: 1 #include<iostream> 2 #incl ...
- pku 2387 Til the Cows Come Home
原来一直没去学spfa,感觉只一个Dij已经很够用了,昨天翻了一下最短路,如果路径中存在负权的话,Dij也只能素手无策,这时spfa就大显身手了.. 实现过程: 我们用数组d记录每个结点的最短路径估计 ...
- 【POJ2387】Til the Cows Come Home (最短路)
题面 Bessie is out in the field and wants to get back to the barn to get as much sleep as possible bef ...
- A - Til the Cows Come Home POJ - 2387
A - Til the Cows Come Home POJ - 2387 最短路 #include<iostream> #include<cstdio> #include&l ...
最新文章
- Datawhale第五期组队学习团队成员
- BurpSuite学习第七节--Sequencer+Decoder
- 面试必问之【数组】篇
- 全面解读php-面向对象
- FFMPEG源码分析(二)
- 第 三 十 八 天:Linux 的 LVM 逻 辑 卷 管 理
- 百度 合肥地区 软件研发工程师 笔试题
- 路由器获取路由后加表原则
- Oracle 正则表达式函数-REGEXP_REPLACE 使用例子
- ESPRIT 2019初学到走心机编程视频教程
- 搬运:php入门安装教程
- 网络安全等级保护合规一览
- 二级mysql真题2019_2019年9月二级MySQL试题(汇总)
- [生存志] 第71节 阖闾五战入郢
- 手游开发神器 cocos2d-x editor 教程聚合和代码下载(持续更新中)
- 网页兼容性调好了么?
- java的前端还是后端_java语言是开发前端还是后端的
- 光遇安卓服务器维护哪天,光遇安卓版什么时候上线 光遇全平台公测具体开服时间...
- 常见电脑的屏幕比例和分辨率详谈
- 关于python报证书验证失败如何解决