Bzoj1631 [usaco Feb] cow party

Source: Internet
Author: User

Number... In fact, I just wrote a spfa board. It feels good...

Each spfa node is over... Everyone else thinks water...

 

 1 /************************************************************** 2     Problem: 1631 3     User: rausen 4     Language: C++ 5     Result: Accepted 6     Time:196 ms 7     Memory:2380 kb 8 ****************************************************************/ 9  10 #include <cstdio>11 #include <cstring>12 #include <algorithm>13  14 using namespace std;15 const int N = 1005;16 struct edge{17     int next, to, v;18 }e[100005];19 int n, m, S, X, Y, Z, tot;20 int q[100005], dis[N], first[N], dis1[N], ANS;21 bool v[N];22  23 inline int read(){24     int x = 0, sgn = 1;25     char ch = getchar();26     while (ch < ‘0‘ || ch > ‘9‘){27         if (ch == ‘-‘) sgn = -1;28         ch = getchar();29     }30     while (ch >= ‘0‘ && ch <= ‘9‘){31         x = x * 10 + ch - ‘0‘;32         ch = getchar();33     }34     return sgn * x;35 }36  37 inline void add_edge(int x, int y, int z){38     e[++tot].next = first[x], first[x] = tot;39     e[tot].to = y, e[tot].v = z;40 }41  42 void spfa(int S){43     memset(dis, 127, sizeof(dis));44     q[0] = S, dis[S] = 0, v[S] = 1;45     int p, x, y;46     for (int l = 0, r = 0; l <= r; ++l){47         p = q[l];48         for (x = first[p]; x; x = e[x].next){49             y = e[x].to;50             if (dis[p] + e[x].v < dis[y]){51                 dis[y] = dis[p] + e[x].v;52                 if (!v[y])53                     v[y] = 1, q[++r] = y;54             }55         }56         v[p] = 0;57     }58 }59  60 int main(){61     n = read(), m = read(), S = read();62     for (int i = 1; i <= m; ++i){63         X = read(), Y = read(), Z = read();64         add_edge(X, Y, Z);65     }66     for (int i = 1; i <= n; ++i){67         spfa(i);68         dis1[i] = dis[S];69     }70     spfa(S);71     for (int i = 1; i <= n; ++i)72         ANS = max(ANS, dis[i] + dis1[i]);73     printf("%d\n", ANS);74     return 0;75 }
View code

(P.s. It seems that the practice is very fast... What about those 20 ms mentality... How is it done ...)

Bzoj1631 [usaco Feb] cow party

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.