POJ - 2387 Til the Cows Come Home (最短路)
2017-03-11 13:33
375 查看
题目链接-Til the Cows Come Home
思路:一看就是dijkstra来着,但是还是出现了不少错误。
1.memset函数使用时要包含头文件cstring
2.menset函数按位替换,所以直接memset个max是不行的。
3.//for(int k =1;(!collected[k])&&k<=N;k++)
for(int k =1;k<=N;k++)
if((!collected[k])&&dist[k]<min)以上这一小段代码,要注意不能采用注释掉的那一种,逻辑不正确,举例:每次从第一个开始遍历,而第一个一定是被collected,直接弹出。事实上这句话需要的逻辑是,遇到收集过的直接跳过,而不是直接弹出,不再循环了。
4.if (distance < graph[a][b]) graph[a][b]=graph[b][a]=distance;
在读入的时候一定要注意加上这个if判断,因为有可能有重路,取小的那个。
以下是完整代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define MAX 9999999
using namespace std;
typedef int vertax;
int graph[1005][1005];
int dist[1005];
bool collected[1005];
void dijkstra(int N)
{
vertax v = 0;
int min = MAX;
dist[1] = 0;
for(int j=1;j<=N;j++)
{
min = MAX;
//for(int k =1;(!collected[k])&&k<=N;k++)
for(int k =1;k<=N;k++)
{
if((!collected[k])&&dist[k]<min)
{
min = dist[k];
v = k;
}
}
collected[v]=true;
for(int m = 1;m<=N;m++)
{
if((dist[v]+graph[v][m]<dist[m]))
{
dist[m] = dist[v]+graph[v][m];
}
}
}
};
int main(void)
{
vertax a,b,N;
int T,distance;
cin>>T>>N;
memset(graph,12,sizeof(graph));
memset(dist,12,sizeof(dist));
memset(collected,false,sizeof(collected));
for(int j =0;j<T;j++)
{
//create graph
cin>>a>>b>>distance;
if (distance < graph[a][b]) graph[a][b]=graph[b][a]=distance;
}
//dijkstra
dijkstra(N);
//output
cout<<dist
<<endl;
return 0;
}
思路:一看就是dijkstra来着,但是还是出现了不少错误。
1.memset函数使用时要包含头文件cstring
2.menset函数按位替换,所以直接memset个max是不行的。
3.//for(int k =1;(!collected[k])&&k<=N;k++)
for(int k =1;k<=N;k++)
if((!collected[k])&&dist[k]<min)以上这一小段代码,要注意不能采用注释掉的那一种,逻辑不正确,举例:每次从第一个开始遍历,而第一个一定是被collected,直接弹出。事实上这句话需要的逻辑是,遇到收集过的直接跳过,而不是直接弹出,不再循环了。
4.if (distance < graph[a][b]) graph[a][b]=graph[b][a]=distance;
在读入的时候一定要注意加上这个if判断,因为有可能有重路,取小的那个。
以下是完整代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define MAX 9999999
using namespace std;
typedef int vertax;
int graph[1005][1005];
int dist[1005];
bool collected[1005];
void dijkstra(int N)
{
vertax v = 0;
int min = MAX;
dist[1] = 0;
for(int j=1;j<=N;j++)
{
min = MAX;
//for(int k =1;(!collected[k])&&k<=N;k++)
for(int k =1;k<=N;k++)
{
if((!collected[k])&&dist[k]<min)
{
min = dist[k];
v = k;
}
}
collected[v]=true;
for(int m = 1;m<=N;m++)
{
if((dist[v]+graph[v][m]<dist[m]))
{
dist[m] = dist[v]+graph[v][m];
}
}
}
};
int main(void)
{
vertax a,b,N;
int T,distance;
cin>>T>>N;
memset(graph,12,sizeof(graph));
memset(dist,12,sizeof(dist));
memset(collected,false,sizeof(collected));
for(int j =0;j<T;j++)
{
//create graph
cin>>a>>b>>distance;
if (distance < graph[a][b]) graph[a][b]=graph[b][a]=distance;
}
//dijkstra
dijkstra(N);
//output
cout<<dist
<<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 【最短路(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(最短路 Dijkstra/spfa)
- 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(最短路SPFA+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(kuangbin带你飞 专题四:最短路)
- 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+优先队列)