zoukankan      html  css  js  c++  java
  • POJ3268(KB4-D spfa)

    Silver Cow Party

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 23426   Accepted: 10691

    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

     
     1 //2017-08-08
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 
     9 using namespace std;
    10 
    11 const int N = 1010;
    12 const int INF = 0x3f3f3f3f;
    13 struct Edge{
    14     int v, w;
    15     Edge(int _v = 0, int _w = 0):v(_v), w(_w){}
    16 };
    17 vector<Edge> E[2][N];
    18 bool vis[N];
    19 int dis[2][N], cnt[N], n, m, x;
    20 
    21 bool spfa(int s, int n, int time){
    22     memset(vis, false, sizeof(vis));
    23     memset(dis[time], INF, sizeof(dis));
    24     memset(cnt, 0, sizeof(cnt));
    25     vis[s] = true;
    26     dis[time][s] = 0;
    27     cnt[s] = 1;
    28     queue<int> q;
    29     q.push(s);
    30     while(!q.empty()){
    31         int u = q.front();
    32         q.pop();
    33         vis[u] = false;
    34         for(int i = 0; i < E[time][u].size(); i++){
    35             int v = E[time][u][i].v;
    36             int w = E[time][u][i].w;
    37             if(dis[time][v] > dis[time][u] + w){
    38                 dis[time][v] = dis[time][u] + w;
    39                 if(!vis[v]){
    40                     vis[v] = true;
    41                     q.push(v);
    42                     if(++cnt[v] > n)return false;
    43                 }
    44             }
    45         }
    46     }
    47     return true;
    48 }
    49 
    50 int main()
    51 {
    52     while(scanf("%d%d%d", &n, &m, &x)!=EOF){
    53         int a, b, c;
    54         while(m--){
    55             scanf("%d%d%d", &a, &b, &c);
    56             E[0][a].push_back(Edge(b, c));
    57             E[1][b].push_back(Edge(a, c));
    58         }
    59         spfa(x, n, 0);
    60         spfa(x, n, 1);
    61         int ans = 0;
    62         for(int i = 1; i <= n; i++)
    63             ans = max(ans, dis[0][i]+dis[1][i]);
    64         printf("%d
    ", ans);
    65     }
    66 
    67     return 0;
    68 }
  • 相关阅读:
    使用gitlab, jenkins搭建CI(持续集成)系统(1) -- 准备环境
    后台开发技术(2)--接入层设计
    后台开发技术(1)--概述
    【Go】学习笔记兼吐槽(1)
    【PyCharm】书签的使用
    【pygame】Python 不到 300 行代码实现俄罗斯方块
    【杂谈】详解医保报销
    【Python 库】requests 详解超时和重试
    【Python 库】读取 .doc、.docx 两种 Word 文件简述及“Word 未能引发事件”错误
    【Python】鲜为人知的功能特性(下)
  • 原文地址:https://www.cnblogs.com/Penn000/p/7305049.html
Copyright © 2011-2022 走看看