POJ1094查分约束,判断关系是否唯一
生活随笔
收集整理的這篇文章主要介紹了
POJ1094查分约束,判断关系是否唯一
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:
? ? ? 給你一些a<b的關系,然后有三組詢問。
1 當前這組之后如果能確定這n個數的大小關系,那么就輸出關系
2 當前時候出現bug,就是和前面如果沖突,那么就不行
3 最后的答案是否是不確定的,就是既沒確定關系,也沒出現bug.
思路:?
? ? ? 這個題目要清楚一點就是處理順序,上面的三個情況可能會出現重疊的情況,那么就按照上面的1 2 3的優先級來處理,至于判斷當前關系是否成立和唯一我用的是差分約束,沒有用拓撲排序,差分約束跑完最短路(或者最長路)沒有死環,就證明沒有bug,而任意點到起點的距離都不重復,那么就是唯一,否則就是當前不能確定,還有就是討論組里面有個人給了兩組數據,我覺得很有用,我就直接粘貼過來吧,為了大家方便理解題意。
分享兩組關鍵性數據:
Posted by MyTalent at 2013-05-08 23:24:07 on Problem 1094
6 6
A<F
B<D
C<E
F<D
D<E
E<F
output:
Inconsistency found after 6 relations.
5 5
A<B
B<C
C<D
D<E
E<A
output:
Sorted sequence determined after 4 relations: ABCDE
第一個例子講述的是:矛盾和多選,優先判斷是否矛盾
第二個例子講述的是:在矛盾之前如果有成功的,算是成功
#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N_node 30
#define N_edge 1000
#define INF 100000000
using namespace std;
typedef struct
{
? ? int to ,cost ,next;
}STAR;
typedef struct
{
? ? int id ,v;
}ANS;
int list[N_node] ,tot;
int mks[N_node] ,mkt[N_node];
int s_x[N_node];
char str[1000+5][5];
STAR E[N_edge];
ANS ans[N_edge];
void add(int a ,int b ,int c)
{
? ? E[++tot].to = b;
? ? E[tot].cost = c;
? ? E[tot].next = list[a];
? ? list[a] = tot;
}
bool camp(ANS a ,ANS b)
{
? ? return a.v < b.v;
}
bool Spfa(int s ,int n)
{
? ? for(int i = 0 ;i <= n ;i ++)
? ? s_x[i] = INF;
? ? memset(mks ,0 ,sizeof(mks));
? ? memset(mkt ,0 ,sizeof(mkt));
? ? queue<int>q;
? ? q.push(s);
? ? s_x[s] = 0;
? ? mks[s] = mkt[s] = 1;
? ? while(!q.empty())
? ? {
? ? ? ? int xin ,tou;
? ? ? ? tou = q.front();
? ? ? ? q.pop();
? ? ? ? mks[tou] = 0;
? ? ? ? for(int k = list[tou] ;k ;k = E[k].next)
? ? ? ? {
? ? ? ? ? ? xin = E[k].to;
? ? ? ? ? ? if(s_x[xin] > s_x[tou] + E[k].cost)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? s_x[xin] = s_x[tou] + E[k].cost;
? ? ? ? ? ? ? ? if(!mks[xin])
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? mks[xin] = 1;
? ? ? ? ? ? ? ? ? ? if(++mkt[xin] >= n) return 0;
? ? ? ? ? ? ? ? ? ? q.push(xin);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? return 1;
}
bool judeok(int n ,int id)
{
? ? for(int i = 1 ;i <= n ;i ++)
? ? {
? ? ? ? ans[i].id = i;
? ? ? ? ans[i].v = s_x[i];
? ? }
? ? sort(ans + 1 ,ans + n + 1 ,camp);
? ? for(int i = 2 ;i <= n ;i ++)
? ? if(ans[i].v == ans[i-1].v)
? ? return 0;
? ? printf("Sorted sequence determined after %d relations: " ,id);
? ? for(int i = 1 ;i <= n ;i ++)
? ? printf("%c" ,ans[i].id + 'A' - 1);
? ? printf(".\n");
? ? return 1;
}
int main ()
{
? ? int n ,m ,i;
? ? while(~scanf("%d %d" ,&n ,&m) && n + m)
? ? {
? ? ? ? for(i = 1 ;i <= m ;i ++)
? ? ? ? scanf("%s" ,str[i]);
? ? ? ? memset(list ,0 ,sizeof(list));
? ? ? ? tot = 1;
? ? ? ? for(i = 1 ;i <= n ;i ++)
? ? ? ? add(0 ,i ,0);//虛擬出來一個0點,連接所有點,為了防止整個圖不是連通的
? ? ? ? for(i = 1 ;i <= m ;i ++)
? ? ? ? {
? ? ? ? ? ? int a = str[i][0] - 'A' + 1;
? ? ? ? ? ? int b = str[i][2] - 'A' + 1;
? ? ? ? ? ? add(b ,a ,-1);
? ? ? ? ? ? int now = Spfa(0 ,n);
? ? ? ? ? ? if(now && judeok(n ,i)) break;
? ? ? ? ? ? if(!now)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? printf("Inconsistency found after %d relations.\n" ,i);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if(i == m + 1)
? ? ? ? {
? ? ? ? ? ? printf("Sorted sequence cannot be determined.\n");
? ? ? ? ? ? continue;
? ? ? ? }
? ? }
? ? return 0;
}
? ? ? 給你一些a<b的關系,然后有三組詢問。
1 當前這組之后如果能確定這n個數的大小關系,那么就輸出關系
2 當前時候出現bug,就是和前面如果沖突,那么就不行
3 最后的答案是否是不確定的,就是既沒確定關系,也沒出現bug.
思路:?
? ? ? 這個題目要清楚一點就是處理順序,上面的三個情況可能會出現重疊的情況,那么就按照上面的1 2 3的優先級來處理,至于判斷當前關系是否成立和唯一我用的是差分約束,沒有用拓撲排序,差分約束跑完最短路(或者最長路)沒有死環,就證明沒有bug,而任意點到起點的距離都不重復,那么就是唯一,否則就是當前不能確定,還有就是討論組里面有個人給了兩組數據,我覺得很有用,我就直接粘貼過來吧,為了大家方便理解題意。
分享兩組關鍵性數據:
Posted by MyTalent at 2013-05-08 23:24:07 on Problem 1094
6 6
A<F
B<D
C<E
F<D
D<E
E<F
output:
Inconsistency found after 6 relations.
5 5
A<B
B<C
C<D
D<E
E<A
output:
Sorted sequence determined after 4 relations: ABCDE
第一個例子講述的是:矛盾和多選,優先判斷是否矛盾
第二個例子講述的是:在矛盾之前如果有成功的,算是成功
#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N_node 30
#define N_edge 1000
#define INF 100000000
using namespace std;
typedef struct
{
? ? int to ,cost ,next;
}STAR;
typedef struct
{
? ? int id ,v;
}ANS;
int list[N_node] ,tot;
int mks[N_node] ,mkt[N_node];
int s_x[N_node];
char str[1000+5][5];
STAR E[N_edge];
ANS ans[N_edge];
void add(int a ,int b ,int c)
{
? ? E[++tot].to = b;
? ? E[tot].cost = c;
? ? E[tot].next = list[a];
? ? list[a] = tot;
}
bool camp(ANS a ,ANS b)
{
? ? return a.v < b.v;
}
bool Spfa(int s ,int n)
{
? ? for(int i = 0 ;i <= n ;i ++)
? ? s_x[i] = INF;
? ? memset(mks ,0 ,sizeof(mks));
? ? memset(mkt ,0 ,sizeof(mkt));
? ? queue<int>q;
? ? q.push(s);
? ? s_x[s] = 0;
? ? mks[s] = mkt[s] = 1;
? ? while(!q.empty())
? ? {
? ? ? ? int xin ,tou;
? ? ? ? tou = q.front();
? ? ? ? q.pop();
? ? ? ? mks[tou] = 0;
? ? ? ? for(int k = list[tou] ;k ;k = E[k].next)
? ? ? ? {
? ? ? ? ? ? xin = E[k].to;
? ? ? ? ? ? if(s_x[xin] > s_x[tou] + E[k].cost)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? s_x[xin] = s_x[tou] + E[k].cost;
? ? ? ? ? ? ? ? if(!mks[xin])
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? mks[xin] = 1;
? ? ? ? ? ? ? ? ? ? if(++mkt[xin] >= n) return 0;
? ? ? ? ? ? ? ? ? ? q.push(xin);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? return 1;
}
bool judeok(int n ,int id)
{
? ? for(int i = 1 ;i <= n ;i ++)
? ? {
? ? ? ? ans[i].id = i;
? ? ? ? ans[i].v = s_x[i];
? ? }
? ? sort(ans + 1 ,ans + n + 1 ,camp);
? ? for(int i = 2 ;i <= n ;i ++)
? ? if(ans[i].v == ans[i-1].v)
? ? return 0;
? ? printf("Sorted sequence determined after %d relations: " ,id);
? ? for(int i = 1 ;i <= n ;i ++)
? ? printf("%c" ,ans[i].id + 'A' - 1);
? ? printf(".\n");
? ? return 1;
}
int main ()
{
? ? int n ,m ,i;
? ? while(~scanf("%d %d" ,&n ,&m) && n + m)
? ? {
? ? ? ? for(i = 1 ;i <= m ;i ++)
? ? ? ? scanf("%s" ,str[i]);
? ? ? ? memset(list ,0 ,sizeof(list));
? ? ? ? tot = 1;
? ? ? ? for(i = 1 ;i <= n ;i ++)
? ? ? ? add(0 ,i ,0);//虛擬出來一個0點,連接所有點,為了防止整個圖不是連通的
? ? ? ? for(i = 1 ;i <= m ;i ++)
? ? ? ? {
? ? ? ? ? ? int a = str[i][0] - 'A' + 1;
? ? ? ? ? ? int b = str[i][2] - 'A' + 1;
? ? ? ? ? ? add(b ,a ,-1);
? ? ? ? ? ? int now = Spfa(0 ,n);
? ? ? ? ? ? if(now && judeok(n ,i)) break;
? ? ? ? ? ? if(!now)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? printf("Inconsistency found after %d relations.\n" ,i);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if(i == m + 1)
? ? ? ? {
? ? ? ? ? ? printf("Sorted sequence cannot be determined.\n");
? ? ? ? ? ? continue;
? ? ? ? }
? ? }
? ? return 0;
}
總結
以上是生活随笔為你收集整理的POJ1094查分约束,判断关系是否唯一的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ1087DFS+匈牙利或者DINI
- 下一篇: POJ1135比较有意思的对短路(多米骨