• POJ 3268 Silver Cow Party (Dijkstra)


    Silver Cow Party
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 13982   Accepted: 6307

    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




    因为迪杰斯特拉求得是一点到其他所有点的最短路径,所以先把起点设成X,跑一次之后算出了每头牛回去的最短路径,然后把边反转,入边变成出边,出边变成入边,然后再把起点设成X,跑一次之后就算出了每头牛来的最短路径,最后把每头牛来回的最短路径加起来,取所有牛的最大值。写得太挫,明天补发其他解法。
    #include <iostream>
    #include <cstdio>
    #include <queue>
    #include <vector>
    using    namespace    std;
    
    const    int    SIZE = 1005;
    const    int    INF = 0x6fffffff;
    int    MAP[SIZE][SIZE];
    bool    VIS[SIZE][SIZE];
    int    BOX[SIZE];
    int    D[SIZE],S[SIZE];
    int    N,M,X;
    struct    Comp
    {
        bool    operator ()(int & a,int & b)
        {
            return    D[a] > D[b];
        }
    };
    struct    Node
    {
        int    vec,cost;
    };
    vector<Node>    G[SIZE];
    
    void    dijkstra(int);
    int    main(void)
    {
        Node    temp;
        int    from;
    
        while(scanf("%d%d%d",&N,&M,&X) != EOF)
        {
            fill(&MAP[0][0],&MAP[SIZE - 1][SIZE - 1],INF);
            fill(&VIS[0][0],&VIS[SIZE - 1][SIZE - 1],false);
            fill(BOX,BOX + SIZE - 1,0);
            for(int i = 1;i <= N;i ++)
                G[i].clear();
            for(int i = 0;i < M;i ++)
            {
                scanf("%d%d%d",&from,&temp.vec,&temp.cost);
                G[from].push_back(temp);
                MAP[from][temp.vec] = temp.cost;
            }
            dijkstra(X);
            for(int i = 1;i <= N;i ++)
                BOX[i] = D[i];
    
            for(int i = 1;i <= N;i ++)
                G[i].clear();
            for(int i = 1;i <= N;i ++)
                for(int j = 1;j <= N;j ++)
                {
                    temp.vec = j;
                    temp.cost = MAP[j][i];
                    G[i].push_back(temp);
                }
            dijkstra(X);
            int    ans = -1;
            for(int i = 1;i <= N;i ++)
            {
                BOX[i] += D[i];
                ans = ans > BOX[i] ? ans : BOX[i];
            }
            printf("%d
    ",ans);
        }
    
        return    0;
    }
    
    void    dijkstra(int s)
    {
        fill(D,D + SIZE,INF);
        fill(S,S + SIZE,false);
        priority_queue<int,vector<int>,Comp>    que;
        D[s] = 0;
        que.push(s);
    
        while(!que.empty())
        {
            int    cur = que.top();
            que.pop();
            S[cur] = true;
    
            for(int i = 0;i < G[cur].size();i ++)
                if(!S[G[cur][i].vec] && D[G[cur][i].vec] > D[cur] + G[cur][i].cost)
                {
                    D[G[cur][i].vec] = D[cur] + G[cur][i].cost;
                    que.push(G[cur][i].vec);
                }
        }
    }

    补发贝尔曼和SPFA

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <queue>
      4 #include <vector>
      5 using    namespace    std;
      6 
      7 const    int    INF = 0x6fffffff;
      8 const    int    SIZE = 1005;
      9 int    N,M,X;
     10 int    MAP[SIZE][SIZE],D[SIZE],BOX[SIZE];
     11 bool    VIS[SIZE];
     12 struct    Node
     13 {
     14     int    vec,cost;
     15 };
     16 vector<Node>    G[SIZE];
     17 
     18 void    SPFA(int);
     19 bool    relax(int,int,int);
     20 int    main(void)
     21 {
     22     int    from,to,cost,ans;
     23     Node    temp;
     24 
     25     while(scanf("%d%d%d",&N,&M,&X) != EOF)
     26     {
     27         fill(BOX,BOX + SIZE,0);
     28         fill(&MAP[0][0],&MAP[SIZE - 1][SIZE - 1],INF);
     29 
     30         for(int i = 0;i < M;i ++)
     31         {
     32             scanf("%d%d%d",&from,&to,&cost);
     33             MAP[from][to] = cost;
     34             temp.vec = to;
     35             temp.cost = cost;
     36             G[from].push_back(temp);
     37         }
     38         SPFA(X);
     39         for(int i = 1;i <= N;i ++)
     40             BOX[i] += D[i];
     41         for(int i = 1;i <= N;i ++)
     42         {
     43             G[i].clear();
     44             for(int j = 1;j <= N;j ++)
     45             {
     46                 if(MAP[j][i] == INF)
     47                     continue;
     48                 temp.vec = j;
     49                 temp.cost = MAP[j][i];
     50                 G[i].push_back(temp);
     51             }
     52         }
     53         SPFA(X);
     54         ans = -1;
     55         for(int i = 1;i <= N;i ++)
     56         {
     57             BOX[i] += D[i];
     58             ans = ans > BOX[i] ? ans : BOX[i];
     59         }
     60         printf("%d
    ",ans);
     61     }
     62 
     63     return    0;
     64 }
     65 
     66 void    SPFA(int s)
     67 {
     68     fill(D,D + SIZE,INF);
     69     fill(VIS,VIS + SIZE,false);
     70     D[s] = 0;
     71     queue<int>    que;
     72     que.push(s);
     73     VIS[s] = true;
     74 
     75     while(!que.empty())
     76     {
     77         int    cur = que.front();
     78         que.pop();
     79         VIS[cur] = false;
     80 
     81         for(int i = 0;i < G[cur].size();i ++)
     82         {
     83             if(relax(cur,G[cur][i].vec,G[cur][i].cost))
     84             {
     85                 que.push(G[cur][i].vec);
     86                 VIS[G[cur][i].vec] = true;
     87             }
     88         }
     89     }
     90 }
     91 
     92 bool    relax(int from,int to,int cost)
     93 {
     94     if(D[to] > D[from] + cost)
     95     {
     96         D[to] = D[from] + cost;
     97         return    true;
     98     }
     99     return    false;
    100 }

    Bellman_ford

    #include <iostream>
    #include <cstdio>
    using    namespace    std;
    
    const    int    INF = 0x6fffffff;
    const    int    SIZE = 1005;
    int    D[SIZE],BOX[SIZE];;
    int    N,M,X;
    struct    Node
    {
        int    from,to,cost;
    }G[SIZE * 100];
    
    void    Bellman_ford(int);
    bool    relax(int,int,int);
    int    main(void)
    {
        int    ans;
    
        while(scanf("%d%d%d",&N,&M,&X) != EOF)
        {
            fill(BOX,BOX + SIZE,0);
            for(int i = 0;i < M;i ++)
                scanf("%d%d%d",&G[i].from,&G[i].to,&G[i].cost);
            Bellman_ford(X);
            for(int i = 1;i <= N;i ++)
                BOX[i] += D[i];
            for(int i = 0;i < M;i ++)
                swap(G[i].from,G[i].to);
            Bellman_ford(X);
            ans = -1;
            for(int i = 1;i <= N;i ++)
            {
                BOX[i] += D[i];
                ans = ans > BOX[i] ? ans : BOX[i];
            }
            printf("%d
    ",ans);
        }
    
        return    0;
    }
    
    void    Bellman_ford(int x)
    {
        fill(D,D + SIZE,INF);
        D[x] = 0;
    
        bool    flag;
        for(int i = 0;i < N - 1;i ++)
        {
            flag = false;
            for(int j = 0;j < M;j ++)
                if(relax(G[j].from,G[j].to,G[j].cost))
                    flag = true;
            if(!flag)
                break;
        }
    }
    
    bool    relax(int from,int to,int cost)
    {
        if(D[to] > D[from] + cost)
        {
            D[to] = D[from] + cost;
            return    true;
        }
        return    false;
    }
  • 相关阅读:
    Android_NDK问题:APP_BUILD_SCRIPT points to an unknown file: <project_path>/jni/Android.mk
    Android开发问题集锦-Button初始为disable状态时自定义的selector不生效问题
    一步步学习Python-django开发-添加后台管理
    一步步学习Python-django开发-建立django数据库
    一步步学习Python-django开发-Mac下搭建Python-Django环境
    JAVA小知识点-Finally和Return的执行关系
    Android自定义组合控件内子控件无法显示问题
    《将博客搬至CSDN》
    idea自动生成try/catch代码块的快捷键
    转:Apache common包 CollectionUtils 使用详解
  • 原文地址:https://www.cnblogs.com/xz816111/p/4495839.html
Copyright © 2020-2023  润新知