PAT 1166 Summit

个人学习记录,代码难免不尽人意。
A summit (峰会) is a meeting of heads of state or government. Arranging the rest areas for the summit is not a simple job. The ideal arrangement of one area is to invite those heads so that everyone is a direct friend of everyone.

Now given a set of tentative arrangements, your job is to tell the organizers whether or not each area is all set.

Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (≤ 200), the number of heads in the summit, and M, the number of friendship relations. Then M lines follow, each gives a pair of indices of the heads who are friends to each other. The heads are indexed from 1 to N.

Then there is another positive integer K (≤ 100), and K lines of tentative arrangement of rest areas follow, each first gives a positive number L (≤ N), then followed by a sequence of L distinct indices of the heads. All the numbers in a line are separated by a space.

Output Specification:
For each of the K areas, print in a line your advice in the following format:

if in this area everyone is a direct friend of everyone, and no friend is missing (that is, no one else is a direct friend of everyone in this area), print Area X is OK…

if in this area everyone is a direct friend of everyone, yet there are some other heads who may also be invited without breaking the ideal arrangement, print Area X may invite more people, such as H. where H is the smallest index of the head who may be invited.

if in this area the arrangement is not an ideal one, then print Area X needs help. so the host can provide some special service to help the heads get to know each other.

Here X is the index of an area, starting from 1 to K.

Sample Input:
8 10
5 6
7 8
6 4
3 6
4 5
2 3
8 2
2 7
5 3
3 4
6
4 5 4 3 6
3 2 8 7
2 2 3
1 1
2 4 6
3 3 2 1
Sample Output:
Area 1 is OK.
Area 2 is OK.
Area 3 is OK.
Area 4 is OK.
Area 5 may invite more people, such as 3.
Area 6 needs help.

#include 
#include
#include
#include
#include
#include 
#include
using namespace std;
const int maxn=210;
const int INF=1000000000;
int G[maxn][maxn];
int d[maxn][maxn];
void floyd(int n){
	for(int k=1;k<=n;k++){
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(G[i][k]!=INF&&G[k][j]!=INF&&d[i][j]>G[i][k]+G[k][j]){
					d[i][j]=G[i][k]+G[k][j];
				}
				
			}
		}
	}
}
int main(){
	for(int i=0;i<maxn;i++){
		for(int j=0;j<maxn;j++){
		
			if(i!=j){
					G[i][j]=INF;
					d[i][j]=INF;
			}
			
			else{
				d[i][j]=0;//需要保证能到达自己且到自己的距离为0
				G[i][j]=0;
			}
			
		}
	}
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=m;i++){
    	int a,b;
    	scanf("%d %d",&a,&b);
    	G[a][b]=1;
    	G[b][a]=1;
	}
	floyd(n);

	int k;
	scanf("%d",&k);
	for(int i=1;i<=k;i++){
		int len;
		scanf("%d",&len);
		int list[len];
		for(int j=0;j<len;j++){
			scanf("%d",&list[j]);
		}
		bool flag=true;
		for(int j=0;j<len-1;j++){
			bool flag1=false;
			for(int m=j+1;m<len;m++){
				if(d[list[j]][list[m]]!=1){//注意不是INF了 
					flag=false;
					flag1=true;
					break;
				}
			}
			if(flag1) break;
		}
		if(flag){
			bool visit[maxn]={0};
			for(int j=0;j<len;j++){
				visit[list[j]]=true;
			}
			int num=-1;
			for(int j=1;j<=n;j++){
				if(!visit[j]&&d[list[0]][j]==1){
					int m=1;
					for(m;m<len;m++){
						if(d[list[m]][j]!=1){
							break;
						}
					}
					if(m==len){
						num=j;
						break;//需要break,否则得到的答案不是最小的 
					} 
				}
			}
			if(num==-1) printf("Area %d is OK.\n",i);
			else
			printf("Area %d may invite more people, such as %d.\n",i,num);
		}
		else{
			printf("Area %d needs help.\n",i);
		}
	}
}

这道题我做麻烦了,我当时提交之后满分通过还挺高兴,后来写博客的时候仔细一想发现根本没有必要用弗洛伊德算法!汗

还是讲讲比较关键的算法思路吧,这道题难的地方在于如何判断是否还有他们的朋友可以在但是没有在area中,我的思路是:我们可以取给的序列第一个作为处理对象,然后遍历所有节点,找到不在序列中的节点(可以用hash表来实现)并且是直接朋友(距离为1)的点,然后依次判断其他在序列中的节点是否和他是直接朋友,如果都是,那么就是这个节点,直接break(如果不break会取到最大节点,题目要最小节点),如果遍历了所有节点还没有找到这样的点,那么就是ok的。

你可能感兴趣的:(PTA,算法,c++,数据结构)