北大ACM2387——Til the Cows Come Home

题目的意思是求解从路标N到路标1的最短路径,简单的最短路径题目,Dijkstra或者Bellman_Ford算法都可以过。

题目有一个坑:输入有重边,所以要选择最小的长度。

下面是AC的代码:

#include <iostream>
#include <cstdio>
using namespace std;

int cost[1005][1005];
bool vis[1005];
int dis[1005];
const int INF = 100000000;
int N, T;

int min(int x, int y)
{
	return x > y ? y : x;
}

void Dijkstra()                  //求最短路径
{
	int i;
	for(i = 1; i <= N; i++)
	{
		vis[i] = false;
		dis[i] = INF;
	}
	dis[N] = 0;
	while(true)
	{
		int v = -1;
		for(i = 1; i <= N; i++)           //从没有选取的顶点中选一个距离最小的顶点
		{
			if(!vis[i] && (v == -1 || dis[i] < dis[v]))
				v = i;
		}
		if(v == -1)                      //全部选取完,退出
			break;
		vis[v] = true;
		for(i = 1; i <= N; i++)          //更新dis
		{
			dis[i] = min(dis[i], dis[v] + cost[v][i]);
		}
	}
}

int main()
{
	int a, b, c;
	while(scanf("%d%d", &T, &N) != EOF)
	{
		for(int j = 0; j <= N; j++)       //初始化cost数组
		{
			for(int k = 0; k <= N; k++)
			{
				if(j != k)
					cost[j][k] = INF;
				else
					cost[j][k] = 0;
			}
		}
		for(int i = 0; i < T; i++)
		{
			scanf("%d%d%d", &a, &b, &c);
			if(cost[a][b] > c)                    //取最小长度
				cost[a][b] = cost[b][a] = c;
		}
		Dijkstra();
		printf("%d\n", dis[1]);
	}
	return 0;
}
时间: 2024-10-08 10:53:19

北大ACM2387——Til the Cows Come Home的相关文章

POJ 2387 Til the Cows Come Home (最短路+Dijkstra)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29550   Accepted: 9935 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

POJ 2387 Til the Cows Come Home dijkstra算法 用邻接表和邻接矩阵

题目如下: Til the Cows Come Home Time Limit: 1000MS        Memory Limit: 65536K Total Submissions: 27726        Accepted: 9353 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wa

POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37662   Accepted: 12836 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

poj 2387 Til the Cows Come Home(dijkstra算法)

题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int map[2010][2010],Min,node[2010],vis[2010],t,q; 5 const int INF=9999999; 6 7 vo

POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Accepted: 15899 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for

POJ 2387 Til the Cows Come Home Dijkstra求最短路径

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 before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.

[2016-04-02][POJ][2387][Til the Cows Come Home]

时间:2016-04-02 10:34:36 星期六 题目编号:[2016-04-02][POJ][2387][Til the Cows Come Home] 题目大意:给定n个节点和t条路,求n到1的最短路长度 分析:跑一次最短路即可 遇到的问题: 据说是多重边,如果是用邻接矩阵的就要更新最小值, 此题是先输入t,再输入n,输入的时候读错,无限WA- #include <queue> #include <cstring> #include <cstdio> using

POj2387——Til the Cows Come Home——————【最短路】

A - Til the Cows Come Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2387 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before F

怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33015   Accepted: 11174 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the