zoukankan      html  css  js  c++  java
  • poj 3268 Silver Cow Party(最短路)

    Silver Cow Party
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 17017   Accepted: 7767

    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.
     
    题意:有n个牛分别住在n个农场,现在要在x农场办party,每个农场的牛都要去参加,所有的牛在去和回来的时候都会选择花费时间最短的路线,现在问,在所有的牛中花费时间最长的是多少时间(注意路径是单向的所以去和回来的路可能不同)
    题解:每头牛都起点到终点,终点到起点跑两次最短路算法,求出最大的
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<cstdio> 
    #include<string>
    #include<math.h>
    #include<algorithm>
    #define LL long long
    #define PI atan(1.0)*4
    #define DD double
    #define MAX 110000
    #define mod 100
    #define dian 1.000000011
    #define INF 0x3f3f3f
    using namespace std;
    int head[MAX],ans;
    int n,m,j,i,t,k,x;
    int a,b,c;
    int vis[MAX],dis[MAX];
    struct node 
    {
    	int u,v,w;
    	int next;
    }edge[MAX];
    void add(int u,int v,int w)
    {
    	edge[ans].u=u;
    	edge[ans].v=v;
    	edge[ans].w=w;
    	edge[ans].next=head[u];
    	head[u]=ans++;
    }
    void getmap()
    {
    	memset(head,-1,sizeof(head));
    	ans=0;
    	for(i=1;i<=m;i++)
    	{
    		scanf("%d%d%d",&a,&b,&c);
    		add(a,b,c);
    	}
    }
    int spfa(int sx,int sy)
    {
        int i,j;
        queue<int>q;
        memset(vis,0,sizeof(vis));
        for(i=1;i<=n;i++)
            dis[i]=INF;
        vis[sx]=1;
        dis[sx]=0;
        q.push(sx);
        while(!q.empty())
        {
            int u=q.front();
                q.pop();
            vis[u]=0;
            for(i=head[u];i!=-1;i=edge[i].next)
            {
                int top=edge[i].v;
                if(dis[top]>dis[u]+edge[i].w)
                {
                    dis[top]=dis[u]+edge[i].w;
                    if(!vis[top])
                    {
                        vis[top]=1;
                        q.push(top);
                    }
                }
            }
        }
        return dis[sy];
    }
    
    void solve()
    {
    	int Max=-INF;
    	int Min=INF;
    	int ant;
    	for(i=1;i<=n;i++)
    	{
    		ant=spfa(i,x)+spfa(x,i);
    		Max=max(Max,ant);
    	}
    	printf("%d
    ",Max);
    } 
    int main()
    {
    	while(scanf("%d%d%d",&n,&m,&x)!=EOF)
    	{
    		getmap();
    		solve();
    	}
    	return 0;
    } 
    

      

  • 相关阅读:
    这一次搞懂Spring自定义标签以及注解解析原理
    为什么要谨慎使用Arrays.asList、ArrayList的subList?
    【踩坑系列】使用long类型处理金额,科学计数法导致金额转大写异常
    小心使用 Task.Run 解惑篇
    小心使用 Task.Run 续篇
    为什么要小心使用 Task.Run
    Visual Studio 调试技巧之即时窗口的妙用
    审计系统的一剂良方——事件溯源
    [C#.NET 拾遗补漏]13:动态构建LINQ查询表达式
    再聊 Blazor,它是否值得你花时间学习
  • 原文地址:https://www.cnblogs.com/tonghao/p/5330504.html
Copyright © 2011-2022 走看看