poj 1094 Sorting It All Out(图论)
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(图论)的更多相关文章
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
- poj 1094 Sorting It All Out (拓扑排序)
http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj 1094 Sorting It All Out 解题报告
题目链接:http://poj.org/problem?id=1094 题目意思:给出 n 个待排序的字母 和 m 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具 ...
- POJ 1094 Sorting It All Out 拓扑排序 难度:0
http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...
- POJ 1094 Sorting It All Out【拓扑排序】
题目链接: http://poj.org/problem?id=1094 题意: 给定前n个字母的大小关系,问你是否 根据前xxx个关系得到上升序列 所有关系都无法确定唯一的一个序列 第xxx个关系导 ...
- poj.1094.Sorting It All Out(topo)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28762 Accepted: 99 ...
- poj 1094 Sorting It All Out(nyoj 349)
点击打开链接 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24544 Accep ...
- POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39602 Accepted: 13 ...
- nyoj 349&Poj 1094 Sorting It All Out——————【拓扑应用】
Sorting It All Out 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 An ascending sorted sequence of distinct ...
随机推荐
- Landsat8数据不同波段组合的用途
2013年2月11日发射的Landsat系列最新卫星Landsat8,携带有OLI陆地成像仪和TIRS热红外传感器,Landsat8的OLI陆地成像仪包括9个波段,OLI包括了ETM+传感器所有的波段 ...
- javascript函数值的重写
原文:javascript函数值的重写 javascript函数值的重写 定义了一个函数,需要重写这个函数并使用原先的函数值.做法是: 1.定义一个变量让原先函数的值指向它,把原先函数的指向一个新的函 ...
- Android仿微信界面--使用Fragment实现(慕课网笔记)
1 效果图 这里我们没有实现滑动切换view的功能 2 具体实现: 2.1 布局文件:top.xml, bottom.xml,tab01.xml,tab02.xml,tab03.xml,tab04. ...
- C++关键字之static
一.面向过程设计中的static 1.静态全局变量 在全局变量前,加上关键字static,该变量就被定义成为一个静态全局变量.我们先举一个静态全局变量的例子,如下: [cpp] #include& ...
- #include <stdint.h>
stdint.h是c99中引进的一个标准C库的头文件. #include<stdio.h> #include<stdint.h> main() { /* 数据类型可以跨平台移植 ...
- 我的经常使用linux小命令
这里并非系统具体介绍每个Linux命令,不过记录本人在平时工作中经经常使用到的一些比較基础的命令及相关的參数,同一时候用了一些简单的样例来说明这些命令的用途,以及怎样用多种命令来实现同一种功能 ...
- UIImage 图片处理:截图,缩放,设定大小,存储
图片的处理大概就分 截图(capture), 缩放(scale),设定大小(resize), 存储(save)这几样比较好处理, 另外还有滤镜,擦试等, 以后再说在这个Demo code裡, 我写了几 ...
- self
self在对象方法中使用,指代调用当前对象方法的对象.可以利用self->属性名称的方法访问成员变量 self在类方法中使用,其指代的是当前类. 使用总结: self 谁调用当前方法,self就 ...
- javascript 全选与反选
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> ...
- 解决windows下Eclipse连接远程Hadoop报错
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.N ...