ZOJ1589(用弗洛伊德做)
#include <iostream>
#include <cstdio>
using namespace std;
const int X_INF = 5;//无穷大
const int MAX_SIZE = 30;
int Graph[MAX_SIZE][MAX_SIZE];
int n,m;
void Init()
{
for(int i = 0; i < 26; i++)
for(int j = 0; j < 26; j++)
if(i == j)
Graph[i][j] = 0;
else
Graph[i][j] = X_INF;
}
void Create()
{
int i;
char a,b,o;
for(i = 0; i < m; i++)
{
cin>>a>>o>>b;
if(o == '<')
Graph[a - 'A'][b - 'A'] = 1;
else
Graph[b - 'A'][a - 'A'] = 1;
}
}
void Floyd()
{
int u,v,w;
for(u = 0; u < 26; u++)
for(v = 0; v < 26; v++)
for(w = 0; w < 26; w++)
if(Graph[v][u] + Graph[u][w] < Graph[v][w])
Graph[v][w] = 2;
}
int main()
{
int t;
bool flag;//标记是否可以推出其他结论
while(cin>>n)
{
for(t = 1; t <= n; t++)
{
cin>>m;
flag = false;
Init();
Create();
Floyd();
cout<<"Case "<<t<<":"<<endl;
for(int i = 0; i < 26; i++)
{
for(int j = 0; j < 26; j++)
if(Graph[i][j] == 2)
{
printf("%c<%c\n",i+'A',j+'A');
flag= true;
}
}
if(!flag)cout<<"NONE"<<endl;
}
}
return 0;
}
#include <cstdio>
using namespace std;
const int X_INF = 5;//无穷大
const int MAX_SIZE = 30;
int Graph[MAX_SIZE][MAX_SIZE];
int n,m;
void Init()
{
for(int i = 0; i < 26; i++)
for(int j = 0; j < 26; j++)
if(i == j)
Graph[i][j] = 0;
else
Graph[i][j] = X_INF;
}
void Create()
{
int i;
char a,b,o;
for(i = 0; i < m; i++)
{
cin>>a>>o>>b;
if(o == '<')
Graph[a - 'A'][b - 'A'] = 1;
else
Graph[b - 'A'][a - 'A'] = 1;
}
}
void Floyd()
{
int u,v,w;
for(u = 0; u < 26; u++)
for(v = 0; v < 26; v++)
for(w = 0; w < 26; w++)
if(Graph[v][u] + Graph[u][w] < Graph[v][w])
Graph[v][w] = 2;
}
int main()
{
int t;
bool flag;//标记是否可以推出其他结论
while(cin>>n)
{
for(t = 1; t <= n; t++)
{
cin>>m;
flag = false;
Init();
Create();
Floyd();
cout<<"Case "<<t<<":"<<endl;
for(int i = 0; i < 26; i++)
{
for(int j = 0; j < 26; j++)
if(Graph[i][j] == 2)
{
printf("%c<%c\n",i+'A',j+'A');
flag= true;
}
}
if(!flag)cout<<"NONE"<<endl;
}
}
return 0;
}