【POJ3268】Silver Cow Party 最短路

题意:一堆奶牛去某个地方,去了又回,然后求去回和的最大值。

题解:两遍最短路,结束,邻接矩阵存边可以避免建反图。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define N  1005
#define inf 0x3f3f3f3f
using namespace std;

int map[N][N],n,m,s;
int dist1[N],dist2[N];
bool visit[N];
void dij1()
{
	memset(dist1,0x3f,sizeof(dist1));
	memset(visit,0,sizeof(visit));
	int i,u,v,temp,round;
	dist1[s]=0;
	for(round=1;round<n;round++)
	{
		temp=inf;
		for(i=1;i<=n;i++)
		{
			if(!visit[i]&&temp>dist1[i])
			{
				temp=dist1[i];
				u=i;
			}
		}
		visit[u]=1;
		for(v=1;v<=n;v++)
		{
			dist1[v]=min(dist1[v],dist1[u]+map[u][v]);
		}
	}
	return ;
}
void dij2()
{
	memset(dist2,0x3f,sizeof(dist2));
	memset(visit,0,sizeof(visit));
	int i,u,v,temp,round;
	dist2[s]=0;
	for(round=1;round<n;round++)
	{
		temp=inf;
		for(i=1;i<=n;i++)
		{
			if(!visit[i]&&temp>dist2[i])
			{
				temp=dist2[i];
				u=i;
			}
		}
		visit[u]=1;
		for(v=1;v<=n;v++)
		{
			dist2[v]=min(dist2[v],dist2[u]+map[v][u]);
		}
	}
	return ;
}

int main()
{
//	freopen("test.in","r",stdin);
	int i,a,b,c;
	scanf("%d%d%d",&n,&m,&s);
	memset(map,0x3f,sizeof(map));
	for(i=1;i<=m;i++)
	{
		scanf("%d%d%d",&a,&b,&c);
		map[a][b]=c;
	}
	dij1();
	dij2();
	int ans=0;
	for(i=1;i<=n;i++)ans=max(ans,dist1[i]+dist2[i]);
	printf("%d\n",ans);
	return 0;
}
时间: 2024-10-21 17:40:29

【POJ3268】Silver Cow Party 最短路的相关文章

POJ3268 Silver Cow Party —— 最短路

题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24527   Accepted: 11164 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co

POJ-3268 Silver Cow Party( 最短路 )

题目链接:http://poj.org/problem?id=3268 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 road

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

POJ3268 Silver Cow Party 【Dijkstra】

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13183   Accepted: 5932 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 

POJ3268 Silver Cow Party(dijkstra+矩阵转置)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15156   Accepted: 6843 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

poj3268——Silver Cow Party(最短路+godv之力)

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

poj3268 Silver Cow Party(最短路)

非常感谢kuangbin专题啊,这道题一开始模拟邻接表做的,反向边不好处理,邻接矩阵的话舒服多了. 题意:给n头牛和m条有向边,每头牛1~n编号,求所有牛中到x编号去的最短路+回来的最短路的最大值. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> using namespace std; const int

poj3268 Silver Cow Party (SPFA求最短路)

其实还是从一个x点出发到所有点的最短路问题.来和回只需分别处理一下逆图和原图,两次SPFA就行了. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #

POJ3268 Silver Cow Party

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18713   Accepted: 8561 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