zoukankan      html  css  js  c++  java
  • uva 515(差分约束)

    题意:告诉你数列中的一段数满足大于或小于不等式关系,让你求是不是存在这样一个数列。

    思路:典型的差分约束的题。主要的步骤是这样的先建图后用SPFA算法判断存不存在负环。

    SPFA算法的具体实现见我的《最短路算法总结》。下面讲一下如何建图首先需要找到一个小于等于的不等式a-b<=c再由b向a连一条权值为c的边。

    至于如何变成这样的不等式只要简单的不等式变换就行了。

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <stack>
     8 #include <vector>
     9 #include <utility>
    10 #define LEN 1010
    11 #define INF 0x3f3f3f3f
    12 #define mp(a, b) make_pair(a, b)
    13 #define pb(a) push_back(a)
    14 
    15 using namespace std;
    16 typedef pair<int, int> pii;
    17 
    18 int n, m, dis[LEN];
    19 vector<pii> Map[LEN];
    20 
    21 void debug(int x[]){
    22     for(int i=0; i<=n; i++){
    23         cout << x[i] << ' ';
    24     }cout << endl;
    25 }
    26 
    27 bool SPFA(int s){
    28     int vis[LEN] = {0}, cnt[LEN] = {0};
    29     queue<int> q;
    30     memset(dis, 0x3f, sizeof dis);
    31     dis[s] = 0;
    32     q.push(s);
    33     vis[s] = 1;
    34     cnt[s]++;
    35     while(!q.empty())
    36     {
    37         int nv = q.front();q.pop();
    38         vis[nv] = 0;
    39         for(int i=0; i<Map[nv].size(); i++){
    40             int x = Map[nv][i].first, y = Map[nv][i].second;
    41             if(dis[x] > dis[nv]+y){
    42                 dis[x] = dis[nv]+y;
    43                 if(!vis[x]){
    44                     vis[x] = 1;
    45                     cnt[x]++;
    46                     if(cnt[x]>n+1) return false;
    47                     q.push(x);
    48                 }
    49             }
    50         }
    51     }
    52     return true;
    53 }
    54 
    55 int main()
    56 {
    57 //    freopen("in.txt", "r", stdin);
    58 
    59     int a, b, c;
    60     char op[10];
    61     while(scanf("%d", &n)!=EOF && n){
    62         scanf("%d", &m);
    63         for(int i=0; i<=LEN; i++)Map[i].clear();
    64         for(int i=0; i<m; i++){
    65             scanf("%d%d%s%d", &a, &b, op, &c);
    66             if(strcmp(op, "gt")==0){
    67                 Map[a+b].pb(mp(a-1, -c-1));
    68             }else{
    69                 Map[a-1].pb(mp(a+b, c-1));
    70             }
    71         }
    72         for(int i=0; i<=n; i++){
    73             Map[n+1].pb(mp(i,1));
    74         }
    75 
    76         if(SPFA(n+1)==true)cout << "lamentable kingdom" << endl;
    77         else cout << "successful conspiracy" << endl;
    78     }
    79     return 0;
    80 }
    View Code
    奔跑吧!少年!趁着你还年轻
  • 相关阅读:
    Spring中bean的生命周期
    HashMap与HashTable原理及数据结构
    HashMap 与HashTable的区别
    2 rocketmq mqadmin 的用法详解
    搭建RocketMQ踩的坑-内存不足
    解决Hash碰撞冲突方法总结
    大端BigEndian、小端LittleEndian与字符集编码
    Linux中最常用的JAVA_HOME配置
    WIP_DISCRETE_JOBS.STATUS_TYPE
    Inventory > INV.MTL_MATERIAL_TRANSACTIONS Show Error Msg: ORA-20100: File lxxx.tmp creation for FND_FILE failed.
  • 原文地址:https://www.cnblogs.com/shu-xiaohao/p/3496711.html
Copyright © 2011-2022 走看看