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



  • 相关阅读:
    ES6 学习笔记(整理一遍阮一峰大神得入门文档,纯自己理解使用)
    怪异模式和标准模式
    计算机网络七层协议模型 “开放系统互联参考模型”,即著名的OSI/RM模型(Open System Interconnection/Reference Model)
    流行得前端构建工具比较,以及gulp配置
    谈谈刚接触sea.js框架得看法
    MAC终端安装grunt--javascript世界得构建工具
    js的数组与对象关系
    JavaScript中的setInterval用法
    每周一题:平方数之和((更新JS)
    每周一题:拿硬币(更新JS)
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834995.html
Copyright © 2011-2022 走看看