PAT 1034. Head of a Gang (30)
2015-03-14 11:29
405 查看
//1034. Head of a Gang (30) //#include "stdafx.h" //case 3 and case 5一直无法通过:主要是没有对结果进行排序 //比较简单的dfs算法了 #include <iostream> #include <algorithm> #include <vector> using namespace std; int Num(string x) { int i; return (x[0]-'A')*26*26 + (x[1] -'A')*26+x[2]-'A'; } vector<int> road[30*30*30]; int N, K; bool visited[30*30*30] = {false}; int Wei[30*30*30] = {0}; vector<int> AAA; vector<int> result; //bool flag = true; void dfs(int curr) { int i; //if (flag == false) //{ // return; //} for (i = 0;i<AAA.size();i++) { if ( curr != AAA[i] && visited[AAA[i]] == false && find(road[curr].begin(), road[curr].end(), AAA[i]) != road[curr].end()) { if (find(result.begin(), result.end(), AAA[i]) == result.end()) { result.push_back(AAA[i]); visited[AAA[i]] = true; //flag = true; dfs(AAA[i]); } } } } void Print(int t) { int tmp1 = t %26; t = t / 26; int tmp2 = t % 26; t = t/26; int tmp3 = t; printf("%c%c%c",tmp3+ 'A', tmp2+'A', tmp1 + 'A'); } typedef struct Gang { int id; int num; }Gang; vector<Gang> gang; bool cmp(Gang g1, Gang g2) { if (g1.id < g2.id) { return true; } return false; } int main() { Gang tmpGang; scanf("%d %d", &N, &K); int i; //string a1, a2; char a1[4], a2[4]; int w; for (i = 0;i<N;i++) { /*scanf("%s %s %d", &a1, &a2, &w);*/ cin >> a1 >> a2 >> w; int t1 = Num(a1); int t2 = Num(a2); if (find(AAA.begin(), AAA.end(), t1) == AAA.end()) { AAA.push_back(t1); } if (find(AAA.begin(), AAA.end(), t2) == AAA.end()) { AAA.push_back(t2); } Wei[t1] += w; Wei[t2] += w; if (find(road[t1].begin(), road[t1].end(), t2) == road[t1].end()) { road[t1].push_back(t2); } if (find(road[t2].begin(), road[t2].end(), t1) == road[t2].end()) road[t2].push_back(t1); } //vector<int> Gang; //vector<int> Gangsize; //对Group进行合并操作 int j; for (i = 0;i<AAA.size();i++) { if (visited[AAA[i]] == true) { continue; } result.clear(); result.push_back(AAA[i]); //flag = true; dfs(AAA[i]); if(result.size() <= 2) continue; int total = 0; //找到当前result里面weight最大的那个。 for (j = 0;j<result.size();j++) { total += Wei[result[j]]; } total /= 2; if (total <= K) { continue; } int maxIndex = -1; int maxValue = 0; for (j = 0;j<result.size();j++) { if (maxIndex == -1 || Wei[result[j]] > maxValue) { maxValue = Wei[result[j]]; maxIndex = result[j]; } } tmpGang.id = maxIndex; tmpGang.num = result.size(); gang.push_back(tmpGang); //Gang.push_back(maxIndex); //Gangsize.push_back(result.size()); //cout <<maxIndex<< " " << result.size() << endl; } cout << gang.size() << endl; sort(gang.begin(), gang.end(), cmp); for (i = 0;i<gang.size();i++) { Print(gang[i].id); cout <<" " << gang[i].num << endl;; } return 0; }
相关文章推荐
- pat甲级1034. Head of a Gang (30)
- 1034. Head of a Gang (30)-PAT甲级真题(图的遍历dfs)
- PAT甲级 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30) 并查集
- PAT 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)【离散化+并查集+搜索】——PAT (Advanced Level) Practise
- Python实现Pat 1034. Head of a Gang (30)
- 【PAT】【Advanced Level】1034. Head of a Gang (30)
- 浙大2012上机 PAT1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30) DFS,删边
- PAT - 甲级 - 1034. Head of a Gang (30)(DFS求连通块)
- PAT (Advanced) 1034. Head of a Gang (30)
- 【PAT甲级】1034. Head of a Gang (30)
- 1034. Head of a Gang (30)-PAT甲级真题
- pat-a1034. Head of a Gang (30)
- 1034. Head of a Gang (30) PAT甲级刷题
- PAT (Advanced Level) 1034. Head of a Gang (30)
- pat 1034. Head of a Gang (30)
- PAT-A-1034. Head of a Gang (30)