zoukankan      html  css  js  c++  java
  • Currency Exchange

    http://poj.org/problem?id=1860

    spfa判断正环

     1 #include <stdio.h>
     2 #include <queue>
     3 #include <string.h>
     4 using namespace std;
     5 const int Max=52050;
     6 
     7 struct node
     8 {
     9     int u;
    10     int v;
    11     double r;
    12     double c;
    13     int next;
    14 } edge[Max];
    15 int head[Max],cnt,vis[Max],pot[Max];
    16 int n,m;
    17 double dis[Max];
    18 
    19 void add(int u,int v,double r,double c)
    20 {
    21     edge[cnt].u = u;
    22     edge[cnt].v = v;
    23     edge[cnt].r = r;
    24     edge[cnt].c = c;
    25     edge[cnt].next = head[u];
    26     head[u] = cnt++;
    27 }
    28 void init()
    29 {
    30     cnt = 0;
    31     memset(head,-1,sizeof(head));
    32     for (int i = 0; i <= n; i ++)
    33     {
    34         dis[i] = 0;
    35     }
    36 }
    37 int relax(int u,int v,double r,double c)
    38 {
    39     if (dis[v] < (dis[u]-c)*r)
    40     {
    41         dis[v] = (dis[u]-c)*r;
    42         return 1;
    43     }
    44     return 0;
    45 }
    46 int spfa(int s,double value)
    47 {
    48     memset(vis,0,sizeof(vis));
    49     memset(pot,0,sizeof(pot));
    50     dis[s] = value;
    51     vis[s] = 1;
    52     ++pot[s];
    53     queue<int>q;
    54     q.push(s);
    55     while(!q.empty())
    56     {
    57         int u = q.front();
    58         q.pop();
    59         vis[u] = 0;
    60         for (int j = head[u]; j != -1; j = edge[j].next)
    61         {
    62             int v = edge[j].v;
    63             double  r = edge[j].r;
    64             double c = edge[j].c;
    65             if (relax(u,v,r,c))
    66             {
    67 
    68                 if(!vis[v])
    69                 {
    70                     if((++pot[v]) > n)
    71                         return 1;
    72                     vis[v] = 1;
    73                     q.push(v);
    74                 }
    75             }
    76         }
    77     }
    78     return 0;
    79 }
    80 int main()
    81 {
    82     int uu,vv,s;
    83     double r1,c1,r2,c2,value;
    84     scanf("%d%d%d%lf",&n,&m,&s,&value);
    85     init();
    86     for (int i = 1; i <= m; i ++)
    87     {
    88         scanf("%d%d%lf%lf%lf%lf",&uu,&vv,&r1,&c1,&r2,&c2);
    89         add(uu,vv,r1,c1);
    90         add(vv,uu,r2,c2);
    91     }
    92     if(spfa(s,value))
    93         printf("YES
    ");
    94     else
    95         printf("NO
    ");
    96     return 0;
    97 }
    View Code
  • 相关阅读:
    Codeforces Round #251 (Div. 2) A
    topcoder SRM 623 DIV2 CatAndRat
    topcoder SRM 623 DIV2 CatchTheBeatEasy
    topcoder SRM 622 DIV2 FibonacciDiv2
    topcoder SRM 622 DIV2 BoxesDiv2
    Leetcode Linked List Cycle II
    leetcode Linked List Cycle
    Leetcode Search Insert Position
    关于vim插件
    Codeforces Round #248 (Div. 2) B. Kuriyama Mirai's Stones
  • 原文地址:https://www.cnblogs.com/lahblogs/p/3248407.html
Copyright © 2011-2022 走看看