您的位置:首页 > 其它

1155. Can I Post the letter(有向图遍历判断两点是否可到达)

2013-01-02 19:56 453 查看
/*  1155. Can I Post the letter(有向图遍历判断两点是否可到达)
题意:寻找可否到达另一个点
思路:每个节点都持有下一个点的下标和持有的下标数destNum
开始时从0开始dfs,发现超时了。从后面n-1往上找。

*/
#include<iostream>
#include<stdlib.h>
#include<vector>
#include<algorithm>
using namespace std;

struct city
{
int destNum;
int dest[200];
};
city cc[200];
int destNum;
bool visit[200];

bool dfs(int index){
if(index == 0){
cout << "I can post the letter" << endl;
return true;
}
visit[index] = true;
for(int i=0; i<cc[index].destNum; i++){
int tempEnd = cc[index].dest[i];
if(!visit[tempEnd])
{
if(dfs(tempEnd))
return true;
}
}
visit[index] = false;
return false;
}

int main()
{
int n, m;

while(cin >> n && n != 0){
cin >> m;
destNum = n-1;
for(int i=0; i<200; i++){
cc[i].destNum=0;
visit[i] = false;
}
for(int i=1; i<=m; i++){
int start,end;
cin >> start >> end;
cc[end].dest[cc[end].destNum++] = start;
}
if(!dfs(n-1)){
cout << "I can't post the letter" << endl;
}
}
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: