zoukankan      html  css  js  c++  java
  • poj_3268Silver Cow Party

    Silver Cow Party
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 10100   Accepted: 4488

    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: 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<cstdio>
    #include<cstring>
    #include<map>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    #include<set>
    #include<string>
    #include<queue>
    #include <stack>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 1005;
    const int INF = 999999;
    int n;
    int maps_come[MAXN][MAXN], maps_back[MAXN][MAXN];
    bool visited[MAXN];
    int dist[MAXN];
    
    void init()
    {
    	for (int i = 1; i <= n; i++)
    	{
    		for (int j = 1; j <= n; j++)
    		{
    			if(i == j)
    			{
    				maps_come[i][j] = 0;
    				maps_back[i][j] = 0;
    			}
    			else
    			{
    				maps_come[i][j] = INF;
    				maps_back[i][j] = INF;
    			}
    		}
    	}
    }
    
    void Dijkstra_back(int s) //起点
    {
    	int i, j;
    	int minValue, minNode;
    
    	dist[s] = 0;
    	visited[s] = true;
    	for (i = 1; i <= n; i++)
    	{
    		dist[i] = maps_back[s][i];
    	}
    	for (i = 1; i <= n; i++)
    	{
    		minValue = INF;
    		minNode = 0;
    		for (j = 1; j <= n; j++)
    		{
    			if(!visited[j] && minValue > dist[j])
    			{
    				minNode = j;
    				minValue = dist[j];
    			}
    		}
    		if(minNode == 0)
    		{
    			break;
    		}
    		visited[minNode] = true;
    		for (j = 1; j <= n; j++)
    		{
    			if(!visited[j] && maps_back[minNode][j] != INF && dist[j] > dist[minNode] + maps_back[minNode][j])
    			{
    				dist[j] = dist[minNode] + maps_back[minNode][j];
    			}
    		}
    	}
    }
    
    void Dijkstra_come(int s) //起点
    {
    	int i, j;
    	int minValue, minNode;
    
    	dist[s] = 0;
    	visited[s] = true;
    	for (i = 1; i <= n; i++)
    	{
    		dist[i] = maps_come[s][i];
    	}
    	for (i = 1; i <= n; i++)
    	{
    		minValue = INF;
    		minNode = 0;
    		for (j = 1; j <= n; j++)
    		{
    			if(!visited[j] && minValue > dist[j])
    			{
    				minNode = j;
    				minValue = dist[j];
    			}
    		}
    		if(minNode == 0)
    		{
    			break;
    		}
    		visited[minNode] = true;
    		for (j = 1; j <= n; j++)
    		{
    			if(!visited[j] && maps_come[minNode][j] != INF && dist[j] > dist[minNode] + maps_come[minNode][j])
    			{
    				dist[j] = dist[minNode] + maps_come[minNode][j];
    			}
    		}
    	}
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int m, x, u, v, w, ans;
    	int temp[MAXN] = {0};
    	scanf("%d %d %d", &n, &m, &x);
    	init();
    	while (m--)
    	{
    		scanf("%d %d %d", &u, &v, &w);
    		if(maps_come[u][v] > w)
    		{
    			maps_come[u][v] = w;
    			maps_back[v][u] = w;
    		}
    	}
    	memset(visited, false, sizeof(visited));
    	Dijkstra_come(x);
    	for (int i = 1; i <= n; i++)
    	{
    		temp[i] += dist[i];
    	}
    	memset(visited, false, sizeof(visited));
    	Dijkstra_back(x);
    	for (int i = 1; i <= n; i++)
    	{
    		temp[i] += dist[i];
    	}
    	ans = -1;
    	for (int i = 1; i <= n; i++)
    	{
    		if(temp[i] > ans)
    		{
    			ans = temp[i];
    		}
    	}
    	printf("%d\n", ans);
    	return 0;
    }



  • 相关阅读:
    [置顶] 内外网同时访问,我的拿来主义
    Nginx防攻击工具教程一 ngx_http_limit_conn_module
    晒晒我的厨艺修炼成果
    在 javascript 中,为什么 [1,2] + [3,4] 不等于 [1,2,3,4]?
    无法解析的外部符号__imp__AlphaBlend@44的解决
    Win32 API实现CDC类的FillSolidRect接口
    pugixml库学习之添加节点
    cleanup failed because the file not under version control问题的解决
    JavaScript 的 typeof 的用途
    支持在Win7和XP系统上创建环境变量的批处理文件
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834995.html
Copyright © 2011-2022 走看看