zoukankan      html  css  js  c++  java
  • TOJ1693(Silver Cow Party)

    Silver Cow Party 分享至QQ空间 去爱问答提问或回答

    Time Limit(Common/Java):2000MS/20000MS     Memory Limit:65536KByte
    Total Submit: 58            Accepted: 28

    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 farmBi, 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

    USACO February 2007

    思路:对原图求一次最短路,再对反图进行一次最短路,然后找最求解即可.

     

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <queue>
    using namespace std;
    const int maxn = 1100;
    const int maxm = 101000;
    const int INF = 0x7fffffff;
    vector<pair<int, int> > g[maxn];
    vector<pair<int, int> > rg[maxn];//reverse;
    int gn, gm, x;
    bool inque[maxn];
    queue<int> Q;
    int d[maxn];//保存原图最短径.
    int rd[maxn];//保存反图的最短路径值.
    
    void spfa2(int s) {
        int i;
        memset(inque, false, sizeof(inque));
        for(i = 1; i < maxn; i++) rd[i] = INF;
        rd[s] = 0;
        while(!Q.empty()) Q.pop();
        Q.push(s);
        inque[s] = true;
        while(!Q.empty()) {
            int u = Q.front();
            Q.pop();
            for(i = 0; i < (int)rg[u].size(); i++) {
                int t = rg[u][i].first;
                if( rd[u] != INF && rd[u] + rg[u][i].second < rd[t]) {
                    rd[t] = rd[u] + rg[u][i].second;
                    if(!inque[t]) {
                        inque[t] = true;
                        Q.push(t);
                    }
                }
            }
            inque[u] = false;
        }
    }
    
    void work(const int s) {
        int i;
        int maxv = -1;
        for(i = 1; i <= gn; i++) {
            if(i != s) {
                if(d[i] + rd[i] > maxv) {
                    maxv = d[i] + rd[i];
                }
            }
        }
        printf("%d
    ", maxv);
    }
    
    void spfa1(int s) {
        int i;
        memset(inque, false, sizeof(inque));
        for(i = 1; i < maxn; i++) d[i] = INF;
        d[s] = 0;
        while(!Q.empty()) Q.pop();
        Q.push(s);
        inque[s] = true;
        while(!Q.empty()) {
            int u = Q.front();
            Q.pop();
            for(i = 0; i < (int)g[u].size(); i++) {
                int t = g[u][i].first;
                if(d[u] != INF && d[u] + g[u][i].second < d[t]) {
                    d[t] = d[u] + g[u][i].second;
                    if(!inque[t]) {
                        inque[t] = true;
                        Q.push(t);
                    }
                }
            }
            inque[u] = false;
        }
    }
    
    int main()
    {
        int i;
        int x, y, w;
        int start;
        pair<int, int> t;
        while(scanf("%d%d%d", &gn, &gm, &start) != EOF) {
            for(i = 1; i <= gn; i++) {
                g[i].clear();
                rg[i].clear();
            }
            for(i = 0; i < gm; i++) {
                scanf("%d%d%d", &x, &y, &w);
                t.first = y;
                t.second = w;
                g[x].push_back(t);
                t.first = x;
                t.second = w;
                rg[y].push_back(t);
            }
            spfa1(start);
            spfa2(start);
            work(start);
        }
        return 0;
    }
  • 相关阅读:
    如何构造分片报文
    Python趣味入门7:循环与条件的爱恨情仇
    如何使用Javascript开发sliding-nav带滑动条效果的导航插件?
    java获取微信用户信息(含源码,直接改下appid就可以使用了)
    数据库设计工具-PowerDesigner中table视图显示name与code
    typora增加到鼠标右键
    基于springboot整合spring-retry
    idea中提交项目到github及invalid authentication data 404 not found not found问题
    git秘钥问题解析及gitlab配置(Please make sure you have the correct access rights and the repository exists)
    idea中打包跳过test
  • 原文地址:https://www.cnblogs.com/riskyer/p/3294013.html
Copyright © 2011-2022 走看看