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

    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 ≤ XN). 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 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.


    题解:还是正向建图和逆向建图。就是求往返路程中最大的一条。


    djistra:

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <queue>
    
    using namespace std;
    
    const int INF= 0x3fffffff;
    
    int map[2][1003][1003];
    bool visited[1003];
    int d[1003];
    int ans[1003];
    
    int max(int a,int b)
    {
    	return a > b ? a : b;
    }
    
    void prim(int n,int s,int flag)
    {
    	memset(visited,false,sizeof(visited));
    	for(int i = 1;i <= n;i++)
    	{
    		d[i] = map[flag][s][i];
    		//cout<<d[i]<<endl;
    	}
    	visited[s] = true;
    	for(int i = 1;i < n;i++)
    	{
    		int min = INF;
    		int k;
    		for(int j = 1;j <= n;j++)
    		{
    			if(!visited[j] && min > d[j])
    			{
    				min = d[j];
    				k = j;
    			}
    		}
    		if(min == INF)
    		{
    			break;
    		}
    		visited[k] = true;
    		for(int j = 1;j <= n;j++)
    		{
    			if(!visited[j] && d[j] > d[k] + map[flag][k][j])
    			{
    				d[j] = d[k] + map[flag][k][j];
    			}
    		}
    	}
    }
    
    int main()
    {
    	int n,m,st;
    	while(scanf("%d%d%d",&n,&m,&st) != EOF)
    	{
    		for(int i = 1;i <= n;i++)
    		{
    			for(int j = 1;j <= n;j++)
    			{
    				if(i == j)
    				{
    					map[0][i][j] = 0;
    					map[1][i][j] = 0;
    				}
    				else
    				{
    					map[0][i][j] = INF;
    					map[1][i][j] = INF;
    				}
    			}
    		}
    		
    		int u,v,c;
    		for(int i = 0;i < m;i++)
    		{
    			scanf("%d%d%d",&u,&v,&c);
    			map[0][u][v] = c;
    			map[1][v][u] = c;
    		}
    		
    		prim(n,st,1);
    		for(int i = 1;i <= n;i++)
    		{
    			ans[i] = d[i];
    		}
    		prim(n,st,0);
    		int res = 0;
    		for(int i = 1;i <= n;i++)
    		{
    			ans[i] += d[i];
    			res = max(res,ans[i]);
    		}
    		
    		printf("%d
    ",res);
    	}
    	
    	
    	return 0;
    }


  • 相关阅读:
    Unity 随机数与随机种子
    Unity 基于Cinemachine计算透视摄像机在地图中的移动范围
    Unity 利用Cinemachine快速创建灵活的相机系统
    Unity NavMesh 动态烘焙绘制与随机取点
    Unity LineRenderer 射线检测 激光攻击
    Unity ugui屏幕适配与世界坐标到ugui屏幕坐标的转换
    实验:用Unity抓取指定url网页中的所有图片并下载保存
    关于Unity中AI随机巡逻障碍物预判与快速运动时物理穿透的思考
    唬人的Java泛型并不难
    你知道Java中的CopyOnWriteArrayList吗?
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/7273295.html
Copyright © 2011-2022 走看看