Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 32933 | Accepted: 11445 |
Description
Input
Output
Sorted sequence determined after xxx relations: yyy...y.
Sorted sequence cannot be determined.
Inconsistency found after xxx relations.
where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence.
Sample Input
4 6 A<B A<C B<C C<D B<D A<B 3 2 A<B B<A 26 1 A<Z 0 0
Sample Output
Sorted sequence determined after 4 relations: ABCD. Inconsistency found after 2 relations. Sorted sequence cannot be determined.
题意:
题意:给定一组字母的大小关系判断它们是否能组成唯一的拓扑序列。
算法:拓扑排序:先判断是否有环,再判断是否有序,最后才能判断是否能得出结果。必须遍历完整个图才能知道是否有环!!!
第一次做拓扑排序类的题,需要知道几个概念:
拓扑排序:
一.定义
对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若<u,v> ∈E(G),则u在线性序列中出现在v之前。
通常,这样的线性序列称为满足拓扑次序(Topological Order)的序列,简称拓扑序列。
注意:
1)只有有向无环图才存在拓扑序列;
2)对于一个DAG,可能存在多个拓扑序列;
如:
该DAG的拓扑序列为A B C D或者A C B D
而此有向图是不存在拓扑序列的,因为图中存在环路
二.拓扑序列算法思想
(1)从有向图中选取一个没有前驱(即入度为0)的顶点,并输出之;
(2)从有向图中删去此顶点以及所有以它为尾的弧;
C语言代码实现:(无队列输出)
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#define MAX 100
usingnamespace std;
void toposort(int map[MAX][MAX],int indegree[MAX],int n)
{
int i,j,k;
for(i=0;i<n;i++) //遍历n次
{
for(j=0;j<n;j++) //找出入度为0的节点
{
if(indegree[j]==0)
{
indegree[j]--;
cout<<j<<endl;
for(k=0;k<n;k++) //删除与该节点关联的边
{
if(map[j][k]==1)
{
indegree[k]--;
}
}
break;
}
}
}
}
int main(void)
{
int n,m; //n:关联的边数,m:节点数
while(scanf("%d %d",&n,&m)==2&&n!=0)
{
int i;
int x,y;
int map[MAX][MAX]; //邻接矩阵
int indegree[MAX]; //入度
memset(map,0,sizeof(map));
memset(indegree,0,sizeof(indegree));
for(i=0;i<n;i++)
{
scanf("%d %d",&x,&y);
if(!map[x][y])
{
map[x][y]=1;
indegree[y]++;
}
}
toposort(map,indegree,m);
}
return0;
}
差分约束系统:
就是给出一些形如x-y<=b不等式的约束,问你是否满足有解的问题,可以转换成图论里的最短路径问题,下面开始详细介绍下
比如给出三个不等式,b-a<=k1,c-b<=k2,c-a<=k3,求出c-a的最大值,我们可以把a,b,c转换成三个点,k1,k2,k3是边上的权,如图
由题我们可以得知,这个有向图中,由题b-a<=k1,c-b<=k2,得出c-a<=k1+k2,因此比较k1+k2和k3的大小,求出最小的就是c-a的最大值了
根据以上的解法,我们可能会猜到求解过程实际就是求从a到c的最短路径,没错的....简单的说就是从a到c沿着某条路径后把所有权值和k求出就是c -a<=k的一个
推广的不等式约束,既然这样,满足题目的肯定是最小的k,也就是从a到c最短距离...
理解了这里之后,想做题还是比较有困难的,因为题目需要变形一下,不能单纯的算..
首先以poj3159为例,这个比较简单,就是给出两个点的最大差,然后让你求1到n的最大差,直接建图后用bellman或者spfa就可以过了
稍微难点的就是poj1364,因为他给出的不等式不是x-y<=k形式,有时候是大于号,这样需要我们去变形一下,并且给出的还是>,<没有等于,都要变形
再有就是poj1201,他要求出的是最长距离,那就要把形式变换成x-y>=k的标准形式
注意点:
1. 如果要求最大值想办法把每个不等式变为标准x-y<=k的形式,然后建立一条从y到x权值为k的边,变得时候注意x-y<k =>x-y<=k-1
如果要求最小值的话,变为x-y>=k的标准形式,然后建立一条从y到x的k边,求出最长路径即可
2.如果权值为正,用dj,spfa,bellman都可以,如果为负不能用dj,并且需要判断是否有负环,有的话就不存在
拓扑排序的核心思想:
拓扑排序的核心就是每次找入度为0的点 进入输出队列 然后将与此点相连的节点入度减1 重复做当做n-1 次后还有点没进输出队列 那么这些点就是环上的 因为环上的各点入度都为1 没有0的 就不能更新。
AC:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int indegree[27]; //入度
int map[27][27]; //图
int queue[27]; // 每次找到入度为0的点后,存储要输出的顺序
int TopoSort(int n) //拓扑排序
{
int count=0; //记录解空间中零入度顶点的个数
int temp[27]; //对入度顶点备份
int pos; //记录一个零入度顶点的数目
int i,j;
int m; //零入度顶点的个数
int flag=1; //flag=1:有序 flag=-1:不能确定
for (i=1; i<=n; i++)
{
temp[i] = indegree[i]; //备份
}
for (i=1; i<=n; i++) //遍历n遍,必须把图全部遍历完(根据当前输入的,尽管map全部初始化为0了,只要根据当前输入的所有条件判断)
{
m = 0;
for (j=1; j<=n; j++) //查找零入度顶点的个数
{
if (temp[j] == 0)
{
m ++;
pos = j; //记录一个零入度顶点的位置
}
}
if (m == 0) //零入度顶点的个数==0:有环
{
return 0;
}
if (m > 1) //零入度顶点的个数>1,说明还有其他两点之间没确定关系,判断是无序(只有1到n个点确定关系,且没有环,根据拓扑排序才能确定有序)
{
flag=-1; //当知道无序时,并不一定知道该图是否有环,因此要继续遍历,可别急着退出...
}
queue[count++] = pos; //零入度顶点入队
temp[pos] = -1; //将零入度顶点的入度置为-1
for (j=1; j<=n; j++) //删除以pos为起点的边
{
if (map[pos][j] == 1)
{
temp[j] --; //相应定点的入度减1
}
}
}
return flag;
}
int main()
{
int n,m;
int sign; //当sign=1时,程序已经得出结果,不需再考虑后面的输入
string str;
while (scanf("%d%d",&n,&m) && n!=0 && m!=0)
{
memset(map,0,sizeof(map));
memset(indegree,0,sizeof(indegree));
sign=0;
for (int i=1; i<=m; i++)
{
cin>>str;
if (sign)
{
continue; //一旦得出结果,对后续的输入置之不理!
}
int u = str[0]-'A'+1;
int v = str[2]-'A'+1;
map[u][v] = 1;
indegree[v] ++;
int s = TopoSort(n);
if (s == 0) //有环
{
printf("Inconsistency found after %d relations.\n",i);
sign=1;
}
if (s == 1) //有序
{
printf("Sorted sequence determined after %d relations: ",i);
for (int j=0; j<n; j++)
{
printf("%c",queue[j]+'A'-1); //根据进队顺序输出字符,整型转字符型
}
printf(".\n");//每句话的结尾的点号,记得输出!
sign=1;
}
}
if(!sign) //无法得出结果
{
printf("Sorted sequence cannot be determined.\n");
}
}
return 0;
}