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;
    }
  • 相关阅读:
    java经典入门算法题,小白必备!
    java客房管理小项目,小白练手必备!
    10道java经典算法题,每一题都能提升你的java水平!第二弹!
    活动目录对象属性批量修改工具------ADModify
    CentOS7基于http方式搭建本地yum源
    如何禁用AD OU 下面的邮箱用户的Exchange ActiveSync 和 适用于设备的OWA
    通过组策略禁止有本地管理员权限的域用户更改网络配置
    1 什么是virtual Machine
    写Ansible playbook添加zabbix被监控的对象
    Windows server 安装和配置zabbix agent
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3295231.html
Copyright © 2011-2022 走看看