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

    Silver Cow Party
    Time Limit: 2000MS
    Memory Limit: 65536K
    Total Submissions: 16277
    Accepted: 7450

    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.

    Source


    错因:错在没有正确的了解时间复杂度吧,看到n==10^3,想到warshall算法的复杂度是
    n^3,算出来为10^9然后看到2s的时间限制就觉得不会超时,后来才知道一般10^8的复杂度才能算1s,就只能换dijkstra了,这样的使用邻接矩阵的复杂度是n^2,就不会超时了

    分析:使用djjkstra的话需要扫两遍,第一次扫了后将边方向全部反过来再扫一遍,关键是将边取反这个操作,写的不好的话又会很复杂,学习了一个新的算法,好好体会下
    <span style="font-size:18px;">void tra()
    {
        int temp;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=i;j++)  //注意是就j<=i
               {
                   temp=map[i][j];
                   map[i][j]=map[j][i];
                   map[j][i]=temp;
               }
    }           </span>
    下面是总的代码:
    <span style="font-size:18px;">#include<cstdio>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <algorithm>
    #include <set>
    using namespace std;
    #define MM(a) memset(a,0,sizeof(a))
    typedef long long LL;
    typedef unsigned long long ULL;
    const int maxn = 1e3+5;
    const int mod = 1000000007;
    const double eps = 1e-10;
    const int inf = 0x3f3f3f3f;
    int map[1005][1005],dist[1005],used[1005],ans[1005];
    int n,m,s;
    void init()
    {
        memset(map,inf,sizeof(map));
        memset(ans,0,sizeof(ans));
        for(int i=1;i<=n;i++)
            map[i][i]=0;
    }
    int min(int a ,int b)
    {
        return a>b?b:a;
    }
    void tra()
    {
        int temp;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=i;j++)
               {
                   temp=map[i][j];
                   map[i][j]=map[j][i];
                   map[j][i]=temp;
               }
    }                             //数组反向
    void dijkstra()
    {
        memset(used,0,sizeof(used));
        memset(dist,inf,sizeof(dist));
        dist[s]=0;
        while(1)
        {
        int u=0,minn=inf;
        for(int i=1;i<=n;i++)
            if(!used[i]&&dist[i]<minn)
            {
                u=i;
                minn=dist[i];
            }
        if(u==0)
            break;
        used[u]=1;
        for(int i=1;i<=n;i++)
            if(!used[i]&&dist[i]>dist[u]+map[u][i])
                dist[i]=dist[u]+map[u][i];
        }
        for(int i=1;i<=n;i++)
            ans[i]+=dist[i];
    }
    int main()
    {
        while(~scanf("%d %d %d",&n,&m,&s))
        {
             init();
             int x,y,c;
             for(int i=1;i<=m;i++)
                {
                    scanf("%d %d %d",&x,&y,&c);
                    map[x][y]=c;
                }
            dijkstra();
            tra();
            dijkstra();
            int maxn=0;
            for(int i=1;i<=n;i++)
                if(ans[i]>maxn)
                maxn=ans[i];
            printf("%d
    ",maxn);
        }
        return 0;
    }
    </span>


  • 相关阅读:
    C#线程并发执行的实例[转]
    Win7怎么用IIS发布网站系统 部署项目
    C#:用SqlBulkCopy来实现批量插入数据
    jQuery 中post 、get的同步问题
    网页配色颜色表(推荐)
    如何修改eclipse的默认字符集和修改中文乱码
    eclipse学习
    SpringMVC开发流程
    SpringMVC的请求
    SpringMVC的数据响应
  • 原文地址:https://www.cnblogs.com/smilesundream/p/6642535.html
Copyright © 2011-2022 走看看