第一次最短路:POJ 2387 Til the Cows Come Home(bfs方法~)
2015-02-04 08:27
447 查看
题目链接:http://poj.org/problem?id=2387
跟随bin神专题刷题,学习了搜索后开始进入最短路之旅,虽然在刚接触ACM就知道鼎鼎大名的最短路,但直到现在我也都没做过……汗~。~……因为做这道题的时候没有看过最短路改怎么求,所以就用搜索做的。。结果水过了。。。。
题意:第一行给你两个整数T,N,分别代表路径数量和终点N,求1到N的最短路。
WA点:注意输入的路径有重复!!
给一组数据:
3 2
1 2 3
1 2 1
1 2 2
答案应该是:
1
用t1[i]表示起点到i点的距离,用Bfs遍历路径,不断地更新t1[i]距离,若能更新则打入队列,为了优化用的vector来存的边,r[i][j]代表与i相连的的第j条边的终点(也就是说 r[u][j]代表以u点为起点,第j条边的终点是r[u][j] )
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<string>
using namespace std;
queue<int> q;
int p;
vector<int> r[2005];
int t1[1005],g[1005][1005];
int u,v,l;
void bfs(){
while(!q.empty()) q.pop();
q.push(1);
t1[1]=0;
while(!q.empty()){
int b=q.front();
q.pop();
for(int i=0;i<r[b].size();i++){
int e=r[b][i];
if(t1[e]==-1){
t1[e]=t1[b]+g[b][e];
q.push(e);
}
else if(t1[e]>t1[b]+g[b][e]){
t1[e]=t1[b]+g[b][e];
q.push(e);
}
}
}
}
int main(){
int t,n;
while(cin>>t>>n){
memset(t1,-1,sizeof(t1));
memset(g,-1,sizeof(g));
for(int i=0;i<2005;i++){
while(r[i].size()) r[i].pop_back();
}
for(int i=0;i<t;i++){
cin>>u>>v>>l;
if(g[u][v]==-1)
g[u][v]=g[v][u]=l;
else if(g[u][v]>l)
g[u][v]=g[v][u]=l;
r[u].push_back(v);
r[v].push_back(u);
}
bfs();
cout<<t1
<<endl;
}
return 0;
}
跟随bin神专题刷题,学习了搜索后开始进入最短路之旅,虽然在刚接触ACM就知道鼎鼎大名的最短路,但直到现在我也都没做过……汗~。~……因为做这道题的时候没有看过最短路改怎么求,所以就用搜索做的。。结果水过了。。。。
题意:第一行给你两个整数T,N,分别代表路径数量和终点N,求1到N的最短路。
WA点:注意输入的路径有重复!!
给一组数据:
3 2
1 2 3
1 2 1
1 2 2
答案应该是:
1
用t1[i]表示起点到i点的距离,用Bfs遍历路径,不断地更新t1[i]距离,若能更新则打入队列,为了优化用的vector来存的边,r[i][j]代表与i相连的的第j条边的终点(也就是说 r[u][j]代表以u点为起点,第j条边的终点是r[u][j] )
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<string>
using namespace std;
queue<int> q;
int p;
vector<int> r[2005];
int t1[1005],g[1005][1005];
int u,v,l;
void bfs(){
while(!q.empty()) q.pop();
q.push(1);
t1[1]=0;
while(!q.empty()){
int b=q.front();
q.pop();
for(int i=0;i<r[b].size();i++){
int e=r[b][i];
if(t1[e]==-1){
t1[e]=t1[b]+g[b][e];
q.push(e);
}
else if(t1[e]>t1[b]+g[b][e]){
t1[e]=t1[b]+g[b][e];
q.push(e);
}
}
}
}
int main(){
int t,n;
while(cin>>t>>n){
memset(t1,-1,sizeof(t1));
memset(g,-1,sizeof(g));
for(int i=0;i<2005;i++){
while(r[i].size()) r[i].pop_back();
}
for(int i=0;i<t;i++){
cin>>u>>v>>l;
if(g[u][v]==-1)
g[u][v]=g[v][u]=l;
else if(g[u][v]>l)
g[u][v]=g[v][u]=l;
r[u].push_back(v);
r[v].push_back(u);
}
bfs();
cout<<t1
<<endl;
}
return 0;
}
相关文章推荐
- POJ 2387 Til the Cows Come Home BFS最短路求解
- poj 2387 Til the Cows Come Home(最短路)
- POJ 2387 Til the Cows Come Home --最短路模板题
- poj 2387 Til the Cows Come Home(最短路SPFA+Dijkstra)
- POJ 2387 Til the Cows Come Home(最短路)
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
- poj 2387 Til the Cows Come Home (最短路,dijkstra模版题)
- POJ 2387 Til the Cows Come Home (最短路+Dijkstra)
- POJ 2387 Til the Cows Come Home (最短路 dijkstra)
- POJ 2387 Til the Cows Come Home (单源最短路SPFA解法)
- 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(最短路Dijkstra+优先队列)
- poj 2387 Til the Cows Come Home -- 最短路dijstra
- 7_4_N题 Til the Cows Come Home 题解[poj 2387] (最短路)
- POJ 2387 || Til the Cows Come Home(最短路,dijkstra
- poj 2387 Til the Cows Come Home(kuangbin带你飞 专题四:最短路)
- POJ 2387 Til the Cows Come Home 最短路