1034. Head of a Gang (30)
2014-11-18 20:51
295 查看
采用dfs的方法来得到每一个连通分量,对每一个连通分量进行筛选。把三个字母化作为26进制的数字转化为int来存储.如果采用邻接矩阵存储在编译时会内存占用过多不通过,所以采用map来存储。
代码如下:
代码如下:
#include<cstdio> #include<map> #include<vector> #include<algorithm> using namespace std; #define N 26*26*26+1 map<int,vector<int> > adjlist; map<int,int> weight; map<int,int> mark; int n,k; map<int,int> res; int maxw,idx,counter,total; void dfs(int n){ mark =1; counter++; total+=weight ; if(weight[idx]<weight )idx=n; vector<int>::iterator it = adjlist .begin(); for(;it!=adjlist .end();it++){ if(mark[*it]==0){ dfs(*it); } } } int main(){ //freopen("1.txt","r",stdin); int i;//,j; char from[4],to[4]; int a,b; int time; scanf("%d %d",&n,&k); for(i=0;i<n;i++){ scanf("%s %s %d",from,to,&time); a=(from[0]-'A')*26*26+(from[1]-'A')*26+from[2]-'A'; b = (to[0]-'A')*26*26+(to[1]-'A')*26+to[2]-'A'; adjlist[a].push_back(b); adjlist[b].push_back(a); weight[a]+=time; weight[b]+=time; mark[a]=0; mark[b]=0; } map<int,int>::iterator it = mark.begin(); for(;it!=mark.end();it++){ if(it->second==0){ idx=it->first; counter=0; total=0; dfs(it->first); if(total/2>k&&counter>2){ res[idx]=counter; } } } printf("%d\n",res.size()); it=res.begin(); int a1,a2,a3; for(;it!=res.end();it++){ a1=it->first/(26*26); a2=it->first%(26*26)/26; a3=it->first%26; printf("%c%c%c %d\n",a1+'A',a2+'A',a3+'A',it->second); } }
相关文章推荐
- 1034. Head of a Gang (30)
- 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) -string离散化 -map应用 -并查集
- PAT甲级1034. Head of a Gang (30)
- PAT-A-1034. Head of a Gang (30)
- 【PAT】【Advanced Level】1034. Head of a Gang (30)
- PAT - 甲级 - 1034. Head of a Gang (30)(DFS求连通块)
- 1034. Head of a Gang (30)
- Python实现Pat 1034. Head of a Gang (30)
- PAT (Advanced) 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)
- 【PAT】1034. Head of a Gang (30)
- 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- 1034. Head of a Gang (30) (并查集 或者 DFS)
- pat 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)