http://poj.org/problem?id=1094

这一题,看了个大牛的解题报告,思路变得非常的清晰:

1,先利用floyd_warshall算法求出图的传递闭包

2,再判断是不是存在唯一的拓扑排序,利用出度和入度是不是相加为n-1

3,利用拓扑排序求出当前的图形的唯一的拓扑排序

一开始我的思路跟上述的差不多,但是没有利用floyd_warshall算法求出传递闭包,准备着利用拓扑排序求出是不是存在着有环回路,我觉得我的这个思路也是可以的。等一下我会将我的这个思路给写成程序。在我的百度云中有这个程序的测试数据(来自poj)

http://pan.baidu.com/disk/home#dir/path=%2Facm%2F%E5%8C%97%E5%A4%A7acm%2F1094

#include <iostream>
//#include <fstream>
using namespace std;
#define MAX 30
/*396K 16MS*/
//var
int a[MAX][MAX];
int n;
int flag1,flag2; //falg1代表的是当前有环的错误,即存在错误的排序
char s[MAX]; //存放最后的结果
//fstream fin;
//function
bool transition();
bool judge();
void toposort(); //main函数
int main()
{
//fin.open("1094.txt",ios::in);
int m;
while(cin>>n>>m)
{
if(n==0&&m==0) break;
memset(a,0,sizeof(a));
int count=1;
flag1=flag2=false;
for(int i=0;i<m;i++)
{
char b1,b2;
cin>>b1>>b2>>b2;
if(flag1||flag2) continue;
if(a[b1-'A'][b2-'A']==0)
{
a[b1-'A'][b2-'A']=1;
//求传递闭包,判断是不是有环,这样就知道是不是存在着错误的答案
if(!transition()){flag1=true;continue;}
//判断是不是存在着唯一的拓扑排序
else if(judge())
{
toposort();
flag2=true;
continue;
}
}
++count;
} if(flag1)
cout<<"Inconsistency found after "<<count<<" relations."<<endl;
else if(flag2)
cout<<"Sorted sequence determined after "<<count<<" relations: "<<s<<"."<<endl;
else
cout<<"Sorted sequence cannot be determined."<<endl;
}
system("pause");
return 0;
} //求传递闭包
bool transition()
{
for(int k=0;k<n;k++)
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(a[i][j]==1||a[i][k]==1&&a[k][j]==1)
a[i][j]=1; for(int i=0;i<n;i++)
if(a[i][i]==1)
return false;
return true;
} //计算是不是存在着唯一的拓扑排序
bool judge()
{
int *ind=new int[n];
int *outd=new int[n];
memset(ind,0,sizeof(int)*n);
memset(outd,0,sizeof(int)*n);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j])
{
ind[j]++;
outd[i]++;
}
}
} for(int i=0;i<n;i++)
if(ind[i]+outd[i]<n-1)
return false;
return true;
} //拓扑排序的实现
void toposort()
{
//按照入度来进行计算
int *ind=new int[n];
memset(ind,0,sizeof(int)*n);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
if(a[i][j]==1)
ind[j]++;
} //求出入度后计算出当前的的拓扑排序的结果
int t=n;
for(int i=0;i<n;i++)
{
int j;
for(j=0;j<n;j++)
if(ind[j]==0)
{ ind[j]--; s[i]=j+'A'; break;}
int t=j;
for(j=0;j<n;j++)
if(a[t][j]==1)
ind[j]--;
} s[n]='\0';
}

poj 1094 Sorting It All Out(图论)的更多相关文章

  1. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

  2. poj 1094 Sorting It All Out (拓扑排序)

    http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  3. poj 1094 Sorting It All Out 解题报告

    题目链接:http://poj.org/problem?id=1094 题目意思:给出 n 个待排序的字母 和 m 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具 ...

  4. POJ 1094 Sorting It All Out 拓扑排序 难度:0

    http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...

  5. POJ 1094 Sorting It All Out【拓扑排序】

    题目链接: http://poj.org/problem?id=1094 题意: 给定前n个字母的大小关系,问你是否 根据前xxx个关系得到上升序列 所有关系都无法确定唯一的一个序列 第xxx个关系导 ...

  6. poj.1094.Sorting It All Out(topo)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28762   Accepted: 99 ...

  7. poj 1094 Sorting It All Out(nyoj 349)

    点击打开链接 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24544   Accep ...

  8. POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39602   Accepted: 13 ...

  9. nyoj 349&Poj 1094 Sorting It All Out——————【拓扑应用】

    Sorting It All Out 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 An ascending sorted sequence of distinct ...

随机推荐

  1. 动态规划-Burst Balloons

    Burst Balloons Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it ...

  2. Oracle ORA-01555 快照过旧 说明

    oracle高级知识(1) ORA-01555 快照过旧,是数据库中很常见的一个错误,比如当我们的事务需要使用undo来构建CR块的时候,而此时对应的undo 已经不存在了, 这个时候就会报ORA-0 ...

  3. matlab绘制函数

    >> x1=linspace(,*pi,); x2=linspace(,*pi,); x3=linspace(,*pi,); y1=sin(x1); y2=+sin(x2); y3=+si ...

  4. android studio 环境配置

    遇到哪些坑: Gradle:configure project 卡死在此处 Haxm is not installed hax is not working and emulator runs in ...

  5. Hibernate 、多表关联映射 - 多对多关系映射(many-to-many)

    hibernate.cfg.xml: <hibernate-configuration> <session-factory name="sessionFactory&quo ...

  6. Java入门基础总结(二)

    判断语句 if else  如下: 1 /*    2                    语法: 3              if(条件) 4              { 5          ...

  7. [ACM] hdu 1003 Max Sum(最大子段和模型)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  8. java 获取两个日期相差的毫秒数

    方法一可以使用date的getTime()方法来将当前日期格式的时间转换为毫秒数,进而相减. long systime = new Date().getTime();//当前系统时间        l ...

  9. QRMaker生成二维码,支持中文

    QRMaker如果想支持中文,可以将中文转为UTF8,然后用InputDateB直接传入Byte() Option Explicit Private Declare Function WideChar ...

  10. jQuery EasyUI combobox多选和赋值

    定义select <select id="ID" name=empVO.acunid class="easyui-combobox" required=& ...