zoukankan      html  css  js  c++  java
  • POJ 3268:Silver Cow Party 求单点的来回最短路径

    Silver Cow Party
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 15989   Accepted: 7303

    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 irequires 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: NM, and X 
    Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time 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.

    求目标点到图中的其他点来回的最小值。

    Dijkstra直接求来回的距离,然后比较求出最小值。

    代码:

    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <string>
    #include <cstring>
    #pragma warning(disable:4996)
    using namespace std;
    
    const int MAX = 100005;
    int edge[1005][1005];
    int vist[1005],vist2[1005],minidis1[1005][1005],minidis2[1005][1005];
    int N,M,X;
    
    void init()
    {
    	int i,j;
    
    	for(i=1;i<=N;i++)
    	{
    		for(j=1;j<=N;j++)
    		{
    			if(j==i)
    			{
    				edge[i][j]=0;
    				minidis1[i][j]=0;
    				minidis2[i][j]=0;
    			}
    			else
    			{
    				edge[i][j]=-1;
    				minidis1[i][j]=MAX;
    				minidis2[i][j]=MAX;
    			}
    		}
    	}
    	for(i=1;i<=N;i++)
    	{
    		vist[i]=0;
    		vist2[i]=0;
    	}
    }
    
    void dijkstra(int i)
    {
    	int j,k;
    	int position=i;
    	int position2=i;
    
    	vist[position]=1;
    	vist2[position]=1;
    	minidis1[i][position]=0;
    	minidis2[position][i]=0;
    
    	for(j=1;j<=N-1;j++)//一共要添加进num-1个点
    	{
    		for(k=1;k<=N;k++)
    		{
    			if(vist[k]==0 && edge[position][k]!=-1 && minidis1[i][position]+edge[position][k] < minidis1[i][k])//新填入的点更新minidis
    			{
    				minidis1[i][k]=minidis1[i][position]+edge[position][k];
    			}
    			if(vist2[k]==0 && edge[k][position2]!=-1 && minidis2[position2][i]+edge[k][position2] < minidis2[k][i])//新填入的点更新minidis
    			{
    				 minidis2[k][i]=minidis2[position2][i]+edge[k][position2];
    			}
    		}
    		int min_value=MAX,min_pos=0;
    		int min_value2=MAX,min_pos2=0;
    		for(k=1;k<=N;k++)
    		{
    			if(vist[k]==0 && minidis1[i][k]<min_value)//比较出最小的那一个作为新添入的店
    			{
    				min_value = minidis1[i][k];
    				min_pos = k;
    			}
    			if(vist2[k]==0 && minidis2[k][i]<min_value2)//比较出最小的那一个作为新添入的店
    			{
    				min_value2 = minidis2[k][i];
    				min_pos2 = k;
    			}
    		}
    
    		vist[min_pos]=1;
    		position=min_pos;
    
    		vist2[min_pos2]=1;
    		position2=min_pos2;
    	}
    
    }
    
    int main()
    {
    	int i;
    	cin>>N>>M>>X;
    	init();
    
    	int temp1,temp2,temp3;
    	for(i=1;i<=M;i++)
    	{
    		cin>>temp1>>temp2>>temp3;
    		edge[temp1][temp2]=temp3;
    	}
    	memset(vist,0,sizeof(vist));
    	memset(vist2,0,sizeof(vist2));
    
    	dijkstra(X);
    	int ans=-1;
    	for(i=1;i<=N;i++)
    	{
    		if(i==X)continue;
    		ans=max(ans,minidis1[X][i]+minidis2[i][X]);
    	}
    
    	cout<<ans<<endl;
    	return 0;
    }



    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    价值理论的出发点和落脚点都是人--以人为本
    价值理论是人类决策和行为的标尺
    事实判断和价值判断
    什么是价值理论?---人们认识世界和改造世界的过程可分解为四个基本阶段
    大人只看利弊 小孩才分对错
    为人处世、事实判断和价值判断皆不可少--人类认识客观事物的标尺:对错与利弊
    知行之间--价值与真理--行动的标尺
    事实判断与价值判断之间的桥梁就是人的需要
    10分钟梳理MySQL核心知识点
    postman设置环境变量,实现一套接口根据选择的环境去请求不同的url
  • 原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4928112.html
Copyright © 2011-2022 走看看