北大ACM3268——Silver Cow Party~~最短路径

这一题的大概题意是:在N个农场中,指定一个农场X,剩余的农场要有牛来到农场X参加Party,每只牛来到农场X会走最短的路径,返回的路径不一定按原路返回,因为每一条路都是单向的。返回也走最短路径。求N - 1 只牛去参加Party到返回自己农场的最短路径和中的最大值。

简单的最短路径,一开始用了Floyd算法,超时了,N最大达到了1000,N^3的复杂度,绝对的超时。

只能换另外的一种算法来求解。

首先分析下题目要我们求解的最短路径和。

从农场X到其他农场的最短路径,可以用Dijkstra算法或者Bellman_Ford算法来求解。表示各个牛返回的最短路径。

而牛到农场X的最短路径如何求解呢,想到这一点就简单了,也就是将各条单向路径的方向调转,也就是本来从农场1到农场2的路径,变成农场2到农场1的路径。然后再次求解农场X到其他农场的最短路径,就求出了其他牛到农场X的最短路径。

下面的是AC的代码,有详细注释:

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

const int INF = 10000000;
int dis1[1005], dis2[1005];               //返回最短路径,过去的最短路径
int cost[1005][1005];
bool vis[1005];                           //标记是否返问过
int N, X, M;

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

void dijkstra(int s, int dis[])           //dijkstra算法求解单源最短路径
{
	for(int i = 0; i <= N; i++)
	{
		vis[i] = false;
		dis[i] = INF;
	}
	dis[s] = 0;
	while(true)
	{
		int v = -1;
		for(int u = 1; u <= N; u++)      //从没有选过的顶点中选取一个距离最短的顶点
		{
			if(!vis[u] && (v < 0 || dis[v] > dis[u]))
				v = u;
		}
		if(v == -1)
			break;
		vis[v] = true;
		for(int j = 1; j <= N; j++)
		{
			dis[j] = min(dis[j], dis[v] + cost[v][j]);
		}
	}
}

int main()
{
//	freopen("data.txt", "r", stdin);
	int i, j, a, b, c;
	while(scanf("%d%d%d", &N, &M, &X) != EOF)
	{
		for(i = 1; i <= N; i++)              //初始化各边权值的数组,
			for(j = 1; j <= N; j++)
			{
				if(i == j)
					cost[i][j] = 0;
				else
					cost[i][j] = INF;
			}
		for(i = 0; i < M; i++)
		{
			scanf("%d%d%d", &a, &b, &c);   //输入各边权值
			cost[a][b] = c;
		}
		dijkstra(X, dis1);                  //算返回最短路径
		for(i = 1; i <= N; i++)            //调转各边,也就是将矩阵转置
		{
			for(j = 1; j < i; j++)
			{
				int temp = cost[i][j];
				cost[i][j] = cost[j][i];
				cost[j][i] = temp;
			}
		}
		dijkstra(X, dis2);                 //算出发最短路径
		int max = -100000;
		for(i = 1; i <= N; i++)            //枚举求最大的最短路径和
		{
			if(i != X)
			{
				int temp = dis1[i] + dis2[i];
				if(max < temp)
					max = temp;
			}
		}
		printf("%d\n", max);
	}
	return 0;
}
时间: 2024-10-10 11:11:55

北大ACM3268——Silver Cow Party~~最短路径的相关文章

&lt;poj - 3268&gt; Silver Cow Party 最短路径问题

本题链接 : http://poj.org/problem?id=3268 题目大意:牛们要去聚会,输入N = 顶点数(牛场):M = 边(路)的数目: X = 终点 (聚会点).问题:求来回时间的最大值.  Input: Line 1: Three space-separated integers, respectively: N, M, and X Lines 2..M+1: Line i+1 describes road i with three space-separated integ

POJ 3268 Silver Cow Party

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20274   Accepted: 9278 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X 

POJ 3268 Silver Cow Party(最短路dijkstra)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15451   Accepted: 6997 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X

洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party

P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads co

luogu P1821 Silver Cow Party

题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires

POJ 3268 Silver Cow Party(SPFA)

Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i re

图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X 

POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects

poj 3268 Silver Cow Party(dijkstra||SPFA)(中等)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14900   Accepted: 6746 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X