【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;
}


版权声明:本文博客原创文章,博客,未经同意,不得转载。






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4660890.html,如需转载请自行联系原作者


相关文章
|
9月前
|
人工智能 算法 BI
D - Silver Cow Party——POJ3268(连续用两次Dijkstra算法)
D - Silver Cow Party——POJ3268(连续用两次Dijkstra算法)
light oj 1231-1232 - 1233- Coin Change 背包
暂时到半懂不懂也没办法讲明白,就不误人子弟了,直接贴代码了。
41 0
P2852 [USACO06DEC]牛奶模式Milk Patterns 后缀数组(poj3261)
P2852 [USACO06DEC]牛奶模式Milk Patterns 后缀数组(poj3261)
124 0
|
机器学习/深度学习 网络架构
PTA 1164-1167 Good in C/Block Reversing/Summit/Cartesian Tree(一)
PTA 1164-1167 Good in C/Block Reversing/Summit/Cartesian Tree
124 0
PTA 1164-1167 Good in C/Block Reversing/Summit/Cartesian Tree(一)
|
机器学习/深度学习 网络架构
PTA 1164-1167 Good in C/Block Reversing/Summit/Cartesian Tree(二)
PTA 1164-1167 Good in C/Block Reversing/Summit/Cartesian Tree
134 0
POJ-Silver Cow Party (最短路)
POJ-Silver Cow Party (最短路)
101 0
|
机器学习/深度学习
AtCoder Beginner Contest 215 F - Dist Max 2 (二分 单调队列)
AtCoder Beginner Contest 215 F - Dist Max 2 (二分 单调队列)
159 0
Optimal Coin Change(完全背包计数)
题目描述 In a 10-dollar shop, everything is worthy 10 dollars or less. In order to serve customers more effectively at the cashier, change needs to be provided in the minimum number of coins. In this problem, you are going to provide a given value of the change in different coins.
101 0

热门文章

最新文章