POJ 2387 Til the Cows Come Home BFS最短路求解
2015-04-14 22:18
99 查看
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=66569#problem/A
题意:给定某些点之间的距离,起终点一定,求回到目的地的最短距离。
以前用的是Djikstra写的,矩阵的最短路写多了,第一想到的就是BFS,Dijkstra的循环写法似乎有点忘记了。习惯性的还是比较喜欢用邻接矩阵来存储,练习下邻接表的写法。
邻接矩阵:
邻接表:
题意:给定某些点之间的距离,起终点一定,求回到目的地的最短距离。
以前用的是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 0x7fffffff using 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 =1;dis =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 0x7fffffff using 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 =1;dis =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 (最短路 dijkstra)
- POJ-2387 Til the Cows Come Home【最短路】
- POJ 2387 Til the Cows Come Home (最短路+Dijkstra)
- 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 最短路
- POJ - 2387 Til the Cows Come Home(最短路Dijkstra模板题)
- poj-2387 Til the Cows Come Home(最短路-spfa)
- 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(最短路 Dijkstra算法)
- 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 [最短路]