zoukankan      html  css  js  c++  java
  • POJ3268 Silver Cow Party

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 18713   Accepted: 8561

    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

     
    二话不说就是一个DFS砸上去,敲一半才看到这图不一定是树……默默删了代码写SPFA。每头牛都要走到X点再走回去,那么就在正向边和反向边上各跑一次SPFA,枚举每个点看,找最长的来回距离和就是答案。
     
     1 /*by SilverN*/
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 #include<queue>
     8 using namespace std;
     9 const int mxm=300010;
    10 const int mxn=2000;
    11 struct edge{
    12     int v,dis;
    13     int next;
    14     bool b;//判断正向反向 
    15 }e[mxm];
    16 int hd[mxm],cnt;
    17 int n,m,x;
    18 int dis[mxn];
    19 int ans[mxn];
    20 void add_edge(int u,int v,int dis){
    21     e[++cnt].v=v;e[cnt].next=hd[u];e[cnt].dis=dis;hd[u]=cnt;
    22     e[cnt].b=1;//正向 
    23     e[++cnt].v=u;e[cnt].next=hd[v];e[cnt].dis=dis;hd[v]=cnt;
    24     e[cnt].b=0;//反向 
    25 }
    26 int inq[mxn];
    27 void SPFA(bool dir){
    28     memset(dis,32,sizeof dis);
    29     queue<int>q;
    30     inq[x]=1;
    31     dis[x]=0;
    32     q.push(x);
    33     while(!q.empty()){
    34         int u=q.front();q.pop();inq[u]=0;
    35         for(int i=hd[u];i;i=e[i].next){
    36             if(e[i].b!=dir)continue;
    37             int v=e[i].v;
    38             if(dis[u]+e[i].dis<dis[v]){
    39                 dis[v]=dis[u]+e[i].dis;
    40                 if(!inq[v]){
    41                     inq[v]=1;
    42                     q.push(v);
    43                 }
    44             }
    45         }
    46     }
    47     return;
    48 }
    49 int main(){
    50     scanf("%d%d%d",&n,&m,&x);
    51     int i,j;
    52     int u,v,d;
    53     for(i=1;i<=m;i++){
    54         scanf("%d%d%d",&u,&v,&d);
    55         add_edge(u,v,d);
    56     }
    57     SPFA(1);//正向 
    58     memcpy(ans,dis,sizeof dis);
    59     SPFA(0);
    60     int mxans=0;
    61     for(i=1;i<=n;i++){
    62         if(dis[i]+ans[i]>mxans)mxans=dis[i]+ans[i];
    63     }
    64     printf("%d
    ",mxans);
    65     return 0;
    66 }
  • 相关阅读:
    小试阿里云容器服务
    .NET跨平台:再见dnx,你好dotnet cli
    使用Let's Encrypt手动创建https证书
    Chrome以https访问gitlab的问题:Your connection is not private
    [ASP.NET 5]终于解决:Unable to load DLL 'api-ms-win-core-localization-obsolete-l1-2-0.dll'
    Mac上远程桌面连接Windows Server 2012 R2
    解决JSON.stringify()在IE10下无法使用的问题
    解决阿里云SLB无法添加https证书的问题
    “状态机”学习笔记
    HttpClient读取ASP.NET Web API错误信息的简单方法
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5774651.html
Copyright © 2011-2022 走看看