zoukankan      html  css  js  c++  java
  • BZOJ1631: [Usaco2007 Feb]Cow Party

    1631: [Usaco2007 Feb]Cow Party

    Time Limit: 5 Sec  Memory Limit: 64 MB
    Submit: 459  Solved: 338
    [Submit][Status]

    Description

        农场有N(1≤N≤1000)个牛棚,每个牛棚都有1只奶牛要参加在X牛棚举行的奶牛派对.共有M(1≤M≤100000)条单向路连接着牛棚,第i条踣需要Ti的时间来通过.牛们都很懒,所以不管是前去X牛棚参加派对还是返回住所,她们都采用了用时最少的路线.那么,用时最多的奶牛需要多少时间来回呢?

    Input

    第1行:三个用空格隔开的整数.

     第2行到第M+1行,每行三个用空格隔开的整数:Ai, Bi,以及Ti.表示一条道路的起点,终点和需要花费的时间.

    Output

    唯一一行:一个整数: 所有参加聚会的奶牛中,需要花费总时间的最大值.

    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

    样例说明:


    共有4只奶牛参加聚会,有8条路,聚会位于第2个农场.


    第4只奶牛可以直接到聚会所在地(花费3时间),然后返程路线经过第1和第3个农场(花费7时间),总共10时间. 

    Source

    题解:
    以s为起点做两次SPFA,第二次把所有边反向
    代码:
     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<iostream>
     7 #include<vector>
     8 #include<map>
     9 #include<set>
    10 #include<queue>
    11 #define inf 1000000000
    12 #define maxn 1000+100
    13 #define maxm 50000+100
    14 #define ll long long
    15 using namespace std;
    16 inline ll read()
    17 {
    18     ll x=0,f=1;char ch=getchar();
    19     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    20     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    21     return x*f;
    22 }
    23 struct edge{int from,go,next,w;}e[2][2*maxm];
    24 int n,m,s,tot[2],q[maxn],d[2][maxn],head[2][maxn];
    25 bool v[maxn];
    26 void ins(int k,int x,int y,int z)
    27 {
    28     e[k][++tot[k]].go=y;e[k][tot[k]].w=z;e[k][tot[k]].next=head[k][x];head[k][x]=tot[k];
    29 }
    30 void spfa(int k)
    31 {
    32     for(int i=1;i<=n;++i) d[k][i]=inf;
    33     memset(v,0,sizeof(v));
    34     int l=0,r=1,x,y;q[1]=s;d[k][s]=0;
    35     while(l!=r)
    36     {
    37         x=q[++l];if(l==maxn)l=0;v[x]=0;
    38         for(int i=head[k][x];i;i=e[k][i].next)
    39          if(d[k][x]+e[k][i].w<d[k][y=e[k][i].go])
    40          {
    41              d[k][y]=d[k][x]+e[k][i].w;
    42              if(!v[y]){v[y]=1;q[++r]=y;if(r==maxn)r=0;}
    43          }
    44     }
    45 }
    46 int main()
    47 {
    48     freopen("input.txt","r",stdin);
    49     freopen("output.txt","w",stdout);
    50     n=read();m=read();s=read();
    51     while(m--)
    52     {
    53         int x=read(),y=read(),z=read();
    54         ins(0,x,y,z);ins(1,y,x,z);
    55     } 
    56     spfa(0);spfa(1);
    57     int ans=0;
    58     for(int i=1;i<=n;i++)ans=max(ans,d[0][i]+d[1][i]);
    59     printf("%d
    ",ans);
    60     return 0;   
    61 }
    View Code
  • 相关阅读:
    在ashx页面中获取session
    写给程序猿们的交互设计
    javascript如何合并多个数组
    <%%>
    #实用# 在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的原因
    通过ContentType = "text/XML" 实现ASP输出xml
    [记录] Host Office Document In WebBrowser control in a Windows Form Application.
    [转载]error LNK2001: unresolved external symbol __DllMainCRTStartup@12错误
    Access database not open
    一些Access MVP们的博客
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/3926924.html
Copyright © 2011-2022 走看看