POJ 3268 Silver Cow Party(Dijkstra)

Silver Cow Party
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

 

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 requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

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 integers:   Ai,   Bi, and   Ti. The described road runs from farm   Ai  to farm   Bi, requiring   Titime units to traverse.

Output

Line 1: One integer: the maximum of time any one cow must walk.

Sample Input

4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3

Sample Output

10

Hint

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.

 

这道题就是说,有N个农场,有M条路,母牛(怎么得出的结论呢?我也不知道,不对,公牛会产奶吗)要到X农场聚会,那么问题来了,每个农场派一条母牛去参加然后都要回到原本的农场,母牛很难,去和回的时候都会走最短路,我们要求出这么多个母牛走的最短路里面距离最长的是多少。对了,这些路都是单向的。所以不一定会原路返回。

一开始我一脸懵逼,用个全局最短路算法如何,就是那个Floyd算法,每个农场之间的最短路都可以求出来,然后这个算法的复杂度是O(N^3),然后N最大是1000,然后就wa了,very happy!其实这就相当于每个点用dijkstra求一次最短路,找到这个点和x点的距离,记录。然后再用x点作为起始点,求出到每个农场的最短路,一相加就行了。这些都是超时的方法,然后我发现了一个很巧妙的方法,写两个dijkstra,其中一个dijkstra中的map判定的行和列全部调转,然后就过了,也不耗时。

两个dijkstra都是从x点开始出发的,其中一个是就按照正常走,也就是参加完宴会回去的距离。还是一个就是将map里的行和列调转,这就相当于是从别的点走过来x,算出距离,就是每个农场到达x的最短距离。两个一相加,找出最大的那个输出就行了。

代码如下:

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

#define MAX 9999999

#define LEN 1005

int map[LEN][LEN]; //某点到某点两点间的的距离
int dist[LEN]; //记录当前点到源点的最短路径长度
int mark[LEN]; //加入进来的点的集合

//初始化map为正无穷大
void init()
{
   	int i,j;
   	for(i=0;i<LEN;i++)
 	{
        for(j=0;j<LEN;j++)
        {
            map[i][j]=MAX;
        }
   	}
}

//n:多少条路 start:起始点 
//dist[i],最后存储着start 到i点的最短距离
void myDijkstra(int n,int start)
{
   	int i,j,min,pos;
   	for(i=1;i<=n;i++)
	{
        mark[i]=0;//没有点加入
        dist[i]=map[start][i];//把start 附近点 dis[]初始化 
   	}
	
	mark[start]=1;//把起始点加进来
	dist[start]=0;

    for(i=1;i<=n;i++)
 	{
        min=MAX;
        for(j=1;j<=n;j++)
        {
            if(!mark[j] && dist[j]<min)
            { //取出不在mark里的最小的dist[i]
                min=dist[j];
                pos=j;//标记
            }
        }
            
        if(min==MAX)//已经不能通了
            break;

        mark[pos]=1;//把K加进来

        //做松弛操作
        for(j=1;j<=n;j++)
        {
            if(!mark[j] && dist[j]>dist[pos]+map[pos][j]) //start->j or start->pos,pos->j 
            {
                dist[j]=dist[pos]+map[pos][j];//这步跟prim算法有点不同
            }
        }
    }
}

void hisDijkstra(int n,int start)
{
   	int i,j,min,pos;
   	for(i=1;i<=n;i++)
	{
        mark[i]=0;//没有点加入
        dist[i]=map[i][start];//把start 附近点 dis[]初始化 
   	}
	
	mark[start]=1;//把起始点加进来
	dist[start]=0;

    for(i=1;i<=n;i++)
 	{
        min=MAX;
        for(j=1;j<=n;j++)
        {
            if(!mark[j] && dist[j]<min)
            { //取出不在mark里的最小的dist[i]
                min=dist[j];
                pos=j;//标记
            }
        }
            
        if(min==MAX)//已经不能通了
            break;

        mark[pos]=1;//把K加进来

        //做松弛操作
        for(j=1;j<=n;j++)
        {
            if(!mark[j] && dist[j]>dist[pos]+map[j][pos]) //start->j or start->pos,pos->j 
            {
                dist[j]=dist[pos]+map[j][pos];//这步跟prim算法有点不同
            }
        }
    }
}

int main()
{	
	int n,m,x,s,e,l,cout[1005]={0};
	init();
	scanf("%d%d%d",&n,&m,&x);
	while(m--)
	{
		scanf("%d%d%d",&s,&e,&l);
		map[s][e] = l;
	}
	myDijkstra(n,x);
	for(int i=1;i<=n;i++)
		cout[i]=dist[i];
	hisDijkstra(n,x);
	for(int i=1;i<=n;i++)
		cout[i]+=dist[i];
	sort(cout+1,cout+n+1);
	printf("%d\n",cout[n]);
}


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值