POJ 2387 Til the Cows Come Home BFS最短路求解
来源:互联网 发布:网络赌钱为什么都是输 编辑:程序博客网 时间:2024/05/08 12:25
题目链接: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;}
0 0
- POJ 2387 Til the Cows Come Home BFS最短路求解
- POJ 2387Til the Cows Come Home(最短路)
- POJ 2387 Til the Cows Come Home 最短路-Dijkstra
- POJ 2387 || Til the Cows Come Home(最短路,dijkstra
- poj 2387 Til the Cows Come Home(最短路)
- poj 2387 Til the Cows Come Home -- 最短路dijstra
- POJ 2387 Til the Cows Come Home 最短路
- POJ 2387 Til the Cows Come Home【最短路】
- POJ 2387 Til the Cows Come Home (最短路)
- poj 2387 Til the Cows Come Home (最短路)
- poj 2387 Til the Cows Come Home (最短路)
- POJ 2387 Til the Cows Come Home 最短路 Dijstra
- POJ-2387 Til the Cows Come Home【最短路】
- POJ-2387-Til the Cows Come Home [最短路]
- 第一次最短路:POJ 2387 Til the Cows Come Home(bfs方法~)
- poj Til the Cows Come Home (Dijkstra最短路~)
- 最短路----Til the Cows Come Home
- Til the Cows Come Home 【最短路】
- Java面试题全集(下)
- 步步为营_Android开发课[30]_用户界面之ViewFlipper(视图切换)
- 别人家的oracle utl_smtp 发送邮件
- Ubuntu14.04下安装OpenCV3.0经验
- strust2学习笔记1
- POJ 2387 Til the Cows Come Home BFS最短路求解
- 黑马程序员——java基础知识之网络编程(一)
- 01背包
- C++中的临时对象用法总结
- 线程池总结[JDK1.6实现]
- c++类的STL使用注意事项
- 黑马程序员——java中的面向对象简介
- 编写自动查找AP上下线日志经验(1)
- C++ set容器元素的插入与遍历