• poj_1364King


    King
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 8593   Accepted: 3240

    Description

    Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ``If my child was a son and if only he was a sound king.'' After nine months her child was born, and indeed, she gave birth to a nice son. 
    Unfortunately, as it used to happen in royal families, the son was a little retarded. After many years of study he was able just to add integer numbers and to compare whether the result is greater or less than a given integer number. In addition, the numbers had to be written in a sequence and he was able to sum just continuous subsequences of the sequence. 

    The old king was very unhappy of his son. But he was ready to make everything to enable his son to govern the kingdom after his death. With regards to his son's skills he decided that every problem the king had to decide about had to be presented in a form of a finite sequence of integer numbers and the decision about it would be done by stating an integer constraint (i.e. an upper or lower limit) for the sum of that sequence. In this way there was at least some hope that his son would be able to make some decisions. 

    After the old king died, the young king began to reign. But very soon, a lot of people became very unsatisfied with his decisions and decided to dethrone him. They tried to do it by proving that his decisions were wrong. 

    Therefore some conspirators presented to the young king a set of problems that he had to decide about. The set of problems was in the form of subsequences Si = {aSi, aSi+1, ..., aSi+ni} of a sequence S = {a1, a2, ..., an}. The king thought a minute and then decided, i.e. he set for the sum aSi + aSi+1 + ... + aSi+ni of each subsequence Si an integer constraint ki (i.e. aSi + aSi+1 + ... + aSi+ni < ki or aSi + aSi+1 + ... + aSi+ni > ki resp.) and declared these constraints as his decisions. 

    After a while he realized that some of his decisions were wrong. He could not revoke the declared constraints but trying to save himself he decided to fake the sequence that he was given. He ordered to his advisors to find such a sequence S that would satisfy the constraints he set. Help the advisors of the king and write a program that decides whether such a sequence exists or not. 

    Input

    The input consists of blocks of lines. Each block except the last corresponds to one set of problems and king's decisions about them. In the first line of the block there are integers n, and m where 0 < n <= 100 is length of the sequence S and 0 < m <= 100 is the number of subsequences Si. Next m lines contain particular decisions coded in the form of quadruples si, ni, oi, ki, where oi represents operator > (coded as gt) or operator < (coded as lt) respectively. The symbols si, ni and ki have the meaning described above. The last block consists of just one line containing 0.

    Output

    The output contains the lines corresponding to the blocks in the input. A line contains text successful conspiracy when such a sequence does not exist. Otherwise it contains text lamentable kingdom. There is no line in the output corresponding to the last ``null'' block of the input.

    Sample Input

    4 2
    1 2 gt 0
    2 2 lt 2
    1 2
    1 0 gt 0
    1 0 lt 0
    0

    Sample Output

    lamentable kingdom
    successful conspiracy
    
    
    是否满足差分约束系统。
    1、设置超级原点
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    #include<set>
    #include<string>
    #include<queue>
    #include <stack>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 105;
    const int INF = 999999;
    
    typedef struct Node 
    {
    	int v;//终点位置
    	int value;//权值
    	int next;//同一起点下在edge数组中的位置
    }Node;
    Node edge[MAXN * 10];//邻接表
    int first[MAXN];//以该点为起点的第一条边在edge数组中的位置
    bool visited[MAXN];
    int dist[MAXN];
    queue<int>Q;
    int index;
    
    void addedge(int u, int v, int w)
    {
    	edge[index].v = v;
    	edge[index].value = w;
    	edge[index].next = first[u];
    	first[u] = index++;
    }
    
    void init()
    {
    	int x, y, w, m;
    	index = 1;
    	memset(first, -1, sizeof(first));
    	char str[3];
    	scanf("%d", &m);
    	while (m--)
    	{
    		scanf("%d %d %s %d", &x, &y, str, &w); //T[si+ni] - T[si-1] <= ki - 1  
    		if(strcmp(str, "gt") == 0) //T[si-1] - T[si+ni] <= -ki - 1  
    		{
    			addedge(x + y + 1, x, -w - 1);
    		}
    		else
    		{
    			addedge(x, x + y + 1, w - 1);
    		}
    	}
    }
    
    
    int SPFA(int Start, int n)
    {
    	int in[MAXN] = {0};
    	while (!Q.empty())
    	{
    		Q.pop();
    	}
    	for (int i = 0; i <= n; i++)
    	{
    		visited[i] = false;
    		dist[i] = INF;
    	}
    	dist[Start] = 0;
    	visited[Start] = true;
    	in[Start]++;
    	Q.push(Start);
    	while (!Q.empty())
    	{
    		int top = Q.front();
    		Q.pop();
    		visited[top] = false;
    		for (int i = first[top]; i != -1 ; i = edge[i].next)
    		{
    			int e = edge[i].v;
    			if(dist[e] > edge[i].value + dist[top])
    			{
    				dist[e] = edge[i].value + dist[top];
    				if(!visited[e])
    				{
    					visited[e] = true;
    					Q.push(e);
    					in[e]++;
    					if(in[e] > n)//入队大于N说明有负环,不满足差分约束系统。
    					{
    						return -1;
    					}
    				}
    			}
    		}
    	}
    	return 1;
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int n;
    	while (scanf("%d", &n) != EOF)
    	{
    		if(n == 0)
    		{
    			break;
    		}
    		init();
    		for (int i = 1; i <= n + 1; i++)
    		{
    			addedge(0, i, 0);
    		}
    		int ans = SPFA(0, n + 1);
    		if(ans == -1)
    			printf("successful conspiracy\n");
    		else
    			printf("lamentable kingdom\n");
    	}
    	return 0;
    }
    
    2、所有元素先入队
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    #include<set>
    #include<string>
    #include<queue>
    #include <stack>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 105;
    const int INF = 999999;
    
    typedef struct Node 
    {
    	int v;//终点位置
    	int value;//权值
    	int next;//同一起点下在edge数组中的位置
    }Node;
    Node edge[MAXN * 10];//邻接表
    int first[MAXN];//以该点为起点的第一条边在edge数组中的位置
    bool visited[MAXN];
    int dist[MAXN];
    queue<int>Q;
    int index;
    int n;
    
    void addedge(int u, int v, int w)
    {
    	edge[index].v = v;
    	edge[index].value = w;
    	edge[index].next = first[u];
    	first[u] = index++;
    }
    
    void init()
    {
    	int x, y, w, m;
    	index = 1;
    	memset(first, -1, sizeof(first));
    	char str[3];
    	scanf("%d", &m);
    	while (m--)
    	{
    		scanf("%d %d %s %d", &x, &y, str, &w); //T[si+ni] - T[si-1] <= ki - 1  
    		if(strcmp(str, "gt") == 0) //T[si-1] - T[si+ni] <= -ki - 1  
    		{
    			addedge(x - 1, x + y, -w - 1);
    		}
    		else
    		{
    			addedge(x + y, x - 1, w - 1);
    		}
    	}
    }
    
    
    int SPFA()
    {
    	int in[MAXN] = {0};
    	while (!Q.empty())
    	{
    		Q.pop();
    	}
    	for (int i = 0; i <= n; i++)
    	{
    		Q.push(i);
    		visited[i] = false;
    		dist[i] = INF;
    	}
    	while (!Q.empty())
    	{
    		int top = Q.front();
    		Q.pop();
    		visited[top] = false;
    		for (int i = first[top]; i != -1 ; i = edge[i].next)
    		{
    			int e = edge[i].v;
    			if(dist[e] > edge[i].value + dist[top])
    			{
    				dist[e] = edge[i].value + dist[top];
    				if(!visited[e])
    				{
    					visited[e] = true;
    					Q.push(e);
    					in[e]++;
    					if(in[e] > n)
    					{
    						return -1;
    					}
    				}
    			}
    		}
    	}
    	return 1;
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	while (scanf("%d", &n) != EOF)
    	{
    		if(n == 0)
    		{
    			break;
    		}
    		init();
    		int ans = SPFA();
    		if(ans == -1)
    			printf("successful conspiracy\n");
    		else
    			printf("lamentable kingdom\n");
    	}
    	return 0;
    }
    



  • 相关阅读:
    经典测试面试题一
    自定义函数实现字符串数组互转
    php 实现密码错误三次锁定账号10分钟
    HTTP状态码
    Linux知识点
    慢查日志
    curl 采集
    HTTP协议
    docker配置
    mysql数据库索引、存储引擎、事务
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835027.html
Copyright © 2020-2023  润新知