| ||||||||||
Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
数据太弱? 写了个自己没把握的爆搜DFS竟然0ms AC~//1129 Channel Allocation //DFS, 用vector记录某个信道编号被用的情况, 每次当选中此信道时, 查看与vector中的点是否邻接即可 //最先得到的答案即为最优解 #include <iostream> #include <vector> using namespace std; const int SIZE = 30; bool graph[SIZE][SIZE]; char save[SIZE]; int n; int cnt; vector<int> vec[30]; bool suss; bool Select(int k, int deep) { for (int j = 0; j != vec[k].size(); ++j) { if(graph[deep][vec[k][j]]) return false; } return true; } void DFS(int deep) { if(suss) return ; if(deep == n)//处理完所有点 { suss = true; return ; } for (int k = 0; k < n; ++k) { if(suss)//这里要添加,因为当已经取得解时, 仍然会进入循环!! return ; if (Select(k, deep)) { if(vec[k].size() == 0)//被选的信道没被用过 { ++cnt; } vec[k].push_back(deep); DFS(deep+1); } } } int main() { freopen("in.txt", "r", stdin); while (scanf("%d", &n) && n != 0) { int i, j; for(i = 0; i < SIZE; ++i) memset(&graph[i], false, sizeof(graph[i])); cnt = 0; suss = false; for(i = 0; i < n; ++i) vec[i].clear(); for (i = 0; i < n; ++i) { scanf("%s", save); for(j = 2; save[j] != '\0'; ++j) { graph[save[0]-'A'][save[j]-'A'] = true; } } DFS(0); if(cnt == 1) printf("%d channel needed.\n", cnt); else printf("%d channels needed.\n", cnt); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator