一、排序
题目描述
给定 n 个变量和 m 个不等式。其中 n 小于等于 26,变量分别用前 n 的大写英文字母表示。
不等式之间具有传递性,即若 A>B 且 B>C,则 A>C。
请从前往后遍历每对关系,每次遍历时判断:
- 如果能够确定全部关系且无矛盾,则结束循环,输出确定的次序;
- 如果发生矛盾,则结束循环,输出有矛盾;
- 如果循环结束时没有发生上述两种情况,则输出无定解。
输入格式
输入包含多组测试数据。
每组测试数据,第一行包含两个整数 n 和 m。
接下来 m 行,每行包含一个不等式,不等式全部为小于关系。
当输入一行
0 0
时,表示输入终止。输出格式
每组数据输出一个占一行的结果。
结果可能为下列三种之一:
- 如果可以确定两两之间的关系,则输出
"Sorted sequence determined after t relations: yyy...y."
,其中't'
指迭代次数,'yyy...y'
是指升序排列的所有变量。- 如果有矛盾,则输出:
"Inconsistency found after t relations."
,其中't'
指迭代次数。- 如果没有矛盾,且不能确定两两之间的关系,则输出
"Sorted sequence cannot be determined."
。数据范围
2≤n≤26,变量只可能为大写字母 A∼Z。
输入样例1:
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
输出样例1:
Sorted sequence determined after 4 relations: ABCD. Inconsistency found after 2 relations. Sorted sequence cannot be determined.
输入样例2:
6 6 A<F B<D C<E F<D D<E E<F 0 0
输出样例2:
Inconsistency found after 6 relations.
输入样例3:
5 5 A<B B<C C<D D<E E<A 0 0
输出样例3:
Sorted sequence determined after 4 relations: ABCDE.
思路:用数组dist[i][j]的值0和1来表示两个字母之间的关系,0表示两个字母之间的关系目前还不知道,1表示i<j。
C++代码:
#include<iostream>
#include<cstring>
using namespace std;
const int N=26;
bool dist[N][N];
bool st[N];
int n,m;
int check()
{
for(int i=0;i<n;i++)
if(dist[i][i]) return 2;
for(int i=0;i<n;i++)
for(int j=0;j<i;j++)//不遍历到n,是为了防止出现i==j的情况下返回0
if(!dist[i][j]&&!dist[j][i]) return 0;
return 1;
}
char get_min()
{
for(int i=0;i<n;i++)
{
if(!st[i])
{
bool flag=true;
for(int j=0;j<n;j++)
{
if(!st[j]&&dist[j][i])
{
flag=false;
break;
}
}
if(flag)
{
st[i]=true;
return 'A'+i;
}
}
}
}
int main()
{
while(cin>>n>>m,n||m)
{
memset(st,false,sizeof st);
memset(dist,0,sizeof dist);
string str;
int type=0,t;
for(int i=1;i<=m;i++)
{
cin>>str;
int a=str[0]-'A',b=str[2]-'A';
if(!type)
{
dist[a][b]=1;
for(int i=0;i<n;i++)
{
if(dist[i][a]) dist[i][b]=1;
if(dist[b][i]) dist[a][i]=1;
for(int j=0;j<n;j++)
if(dist[i][a]&&dist[b][j])
{
dist[i][j]=1;
}
}
type =check();
if(type) t=i;
}
}
if(!type) puts("Sorted sequence cannot be determined.");
else if(type == 1)
{
printf("Sorted sequence determined after %d relations: ",t);
for(int i=0;i<n;i++) printf("%c",get_min());
puts(".");
}
else
{
printf("Inconsistency found after %d relations.\n",t);
}
}
}
二、观光之旅
题目描述
给定一张无向图,求图中一个至少包含 33 个点的环,环上的节点不重复,并且环上的边的长度之和最小。
该问题称为无向图的最小环问题。
你需要输出最小环的方案,若最小环不唯一,输出任意一个均可。
输入格式
第一行包含两个整数 N 和 M,表示无向图有 N 个点,M条边。
接下来 M 行,每行包含三个整数 u,v,l 表示点 u 和点 v 之间有一条边,边长为 l。
输出格式
输出占一行,包含最小环的所有节点(按顺序输出),如果不存在则输出
No solution.
。数据范围
1≤N≤100,
1≤M≤10000,
1≤l<500输入样例:
5 7 1 4 1 1 3 300 3 1 10 1 2 16 2 3 100 2 5 15 5 3 20
输出样例:
1 3 5 2
思路 :用dist[ i ][ j ]+g[ j ][ k ]+g[ k ][ i ]来表示一个环的长度。用pos[ i ][ j ]记录i到j中编号最大的中间节点k。
C++代码:
#include<iostream>
#include<cstring>
using namespace std;
const int N=150;
int pos[N][N],dist[N][N],g[N][N],path[N],cnt;
void get_path(int i,int j)
{
if(pos[i][j]==0) return ;
int k=pos[i][j];
get_path(i,k);
path[cnt++]=k;
get_path(k,j);
}
int main()
{
int n,m;cin>>n>>m;
memset(g,0x3f,sizeof g);
for(int i=1;i<=n;i++) g[i][i]=0;
for(int i=1;i<=m;i++)
{
int a,b,c;cin>>a>>b>>c;
g[a][b]=g[b][a]=min(g[a][b],c);
}
memcpy(dist,g,sizeof dist);
int res=0x3f3f3f3f;
for(int k=1;k<=n;k++)
{
for(int i=1;i<k;i++)
for(int j=i+1;j<k;j++)
{
if(res>(long long)dist[i][j]+g[j][k]+g[k][i])
{
res=dist[i][j]+g[j][k]+g[k][i];
cnt=0;
path[cnt++]=k;
path[cnt++]=i;
get_path(i,j);
path[cnt++]=j;
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(dist[i][j]>dist[i][k]+dist[k][j])
{
dist[i][j]=dist[i][k]+dist[k][j];
pos[i][j]=k;
}
}
}
if(res==0x3f3f3f3f) cout<<"No solution.";
else for(int i=0;i<cnt;i++) cout<<path[i]<<' ';
}