zoukankan      html  css  js  c++  java
  • UVA515 King

    嘟嘟嘟

    题目翻译:有n个数,m个限制条件。每一个限制条件形如:1.x y gt c:表示ax + ax+1 + … +ay > c。2.x y It c:表示ax + ax+1 + …… +ay < c。有解输出“lamentable kingdom”,否则输出“successful conspiracy”。

    对于每一个限制条件,用前缀和的思想,就变成了Sy - Sx-1 > c 和 Sy - Sx-1 < c。于是就是一个典型的差分约束模型。不过差分约束必须满足 ’<=',于是<x就转换成<= x - 1,> x就转换成>= x + 1。

    建好图后跑spfa判负环即可。

    但还有一个问题,就是应该从哪个节点开始?思考一下,我们只用判断图中是否存在负环,因此从那一个点开始都行,那么不放建一个超级源点,向所有点连一条边权为0的边。

      1 #include<cstdio>
      2 #include<iostream>
      3 #include<cmath>
      4 #include<algorithm>
      5 #include<cstring>
      6 #include<cstdlib>
      7 #include<cctype>
      8 #include<vector>
      9 #include<stack>
     10 #include<queue>
     11 using namespace std;
     12 #define enter puts("") 
     13 #define space putchar(' ')
     14 #define Mem(a, x) memset(a, x, sizeof(a))
     15 #define rg register
     16 typedef long long ll;
     17 typedef double db;
     18 const int INF = 0x3f3f3f3f;
     19 const db eps = 1e-8;
     20 const int maxn = 105;
     21 inline ll read()
     22 {
     23     ll ans = 0;
     24     char ch = getchar(), last = ' ';
     25     while(!isdigit(ch)) {last = ch; ch = getchar();}
     26     while(isdigit(ch)) {ans = ans * 10 + ch - '0'; ch = getchar();}
     27     if(last == '-') ans = -ans;
     28     return ans;
     29 }
     30 inline void write(ll x)
     31 {
     32     if(x < 0) x = -x, putchar('-');
     33     if(x >= 10) write(x / 10);
     34     putchar(x % 10 + '0');
     35 }
     36 
     37 int n, m;
     38 char s[2];
     39 struct Edge
     40 {
     41     int to, w, nxt;
     42 }e[maxn << 1];
     43 int head[maxn], ecnt = 0;
     44 void addEdge(int x, int y, int w)
     45 {
     46     e[++ecnt] = (Edge){y, w, head[x]};
     47     head[x] = ecnt;
     48 }
     49 
     50 bool in[maxn];
     51 int dis[maxn], cnt[maxn];
     52 bool spfa(int s)
     53 {
     54     Mem(in, 0); Mem(cnt, 0);
     55     Mem(dis, 0x3f); dis[s] = 0;
     56     queue<int> q; q.push(s);
     57     while(!q.empty())
     58     {
     59         int now = q.front(); q.pop();
     60         in[now] = 0;
     61         for(int i = head[now]; i; i = e[i].nxt)
     62         {
     63             if(dis[e[i].to] > dis[now] + e[i].w)
     64             {
     65                 dis[e[i].to] = dis[now] + e[i].w;
     66                 if(!in[e[i].to])
     67                 {
     68                     in[e[i].to] = 1;
     69                     if(++cnt[e[i].to] > n + 1) return 0;
     70                     q.push(e[i].to);
     71                 }
     72             }
     73         }
     74     }
     75     return 1;
     76 }
     77 
     78 void init()
     79 {
     80     Mem(head, 0);
     81     ecnt = 0;
     82 }
     83 
     84 int main()
     85 {
     86     while(scanf("%d", &n) && n)
     87     {
     88         m = read();
     89         init();
     90         for(int i = 1; i <= m; ++i)
     91         {
     92             int x = read() + 2, y = read(); 
     93             scanf("%s", s); int t = read();
     94             if(s[0] == 'g') addEdge(x + y, x - 1, - t - 1);
     95             else addEdge(x - 1, x + y, t - 1);
     96         }
     97         for(int i = 1; i <= n + 2; ++i) addEdge(0, i, 0);
     98         printf("%s
    ", spfa(0) ? "lamentable kingdom" : "successful conspiracy");
     99     }
    100     return 0;
    101 }
    View Code
  • 相关阅读:
    html5+css3中的background: -moz-linear-gradient 用法 (转载)
    CentOS 安装Apache服务
    Linux 笔记
    CURL 笔记
    Spring Application Context文件没有提示功能解决方法
    LeetCode 389. Find the Difference
    LeetCode 104. Maximum Depth of Binary Tree
    LeetCode 520. Detect Capital
    LeetCode 448. Find All Numbers Disappeared in an Array
    LeetCode 136. Single Number
  • 原文地址:https://www.cnblogs.com/mrclr/p/9791816.html
Copyright © 2011-2022 走看看