PAT Head of a Gang
来源:互联网 发布:淘宝怎么赚运费险 编辑:程序博客网 时间:2024/05/17 03:18
题目大意:对于一个给定的无向图,求出所有的连通分量。还有连通分量中包含的信息:head和总点数。一个连通分量的总边长要大于阈值才合法,head是一个连通分量中相邻边总长度最大的点。输出顺序要按照字典序(姓名)。
思路:采用BFS或DFS都可以求出连通分量。字符串到id映射直接用Map就可以了。注意是无向图,两个点之间不同方向的边算一条边(边长等于两边之和)。
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cstdlib>#include<algorithm>#include<queue>#include<set>#include<map>#include<vector>#include<cmath>#define ll __int64using namespace std;const int INF=0x3fffffff;int n,M,k;int g[2005][2005];bool vis[2005][2005]; //标记边bool vd[2005];//标记点char name[2005][5];int sum;//每个连通分量中的点数int sumTime;//每个连通分量中总通话时间int head;//每个连通分量中相邻点数最多的点struct Gang{ char x[5]; int num; friend bool operator <(Gang g1,Gang g2){ return strcmp(g1.x,g2.x)<0; }}gang[2005];//保存每个连通分量信息(head和sum)void bfs(int x){ int maxCnt=0; queue<int>q; q.push(x); sum++; vd[x]=true; while(!q.empty()){ int s=q.front(); q.pop(); int cnt=0; for(int i=1;i<n;i++){ if(s!=i&&!vis[s][i]&&g[s][i]){ q.push(i); vis[s][i]=vis[i][s]=true; sumTime+=g[s][i]; if(!vd[i]){ vd[i]=true; sum++; } } if(g[s][i]) cnt+=g[s][i]; } if(cnt>maxCnt){ maxCnt=cnt; head=s; } }}int main(){ //freopen("d:\\Test.txt","r",stdin); map<string,int>m; scanf("%d%d",&M,&k); n=1; for(int i=0;i<M;i++){ char s1[5],s2[5]; int w; scanf("%s%s%d",s1,s2,&w); if(m[s1]==0){ m[s1]=n; strcpy(name[n],s1); n++; } if(m[s2]==0){ m[s2]=n; strcpy(name[n],s2); n++; } if(!g[m[s1]][m[s2]]) g[m[s1]][m[s2]]=g[m[s2]][m[s1]]=w; else g[m[s1]][m[s2]]=g[m[s2]][m[s1]]=g[m[s1]][m[s2]]+w; } memset(vis,false,sizeof(vis)); memset(vd,false,sizeof(vd)); int t=0; for(int i=1;i<n;i++){ sum=0; sumTime=0; if(!vd[i]){ bfs(i); //cout<<sum<<" "<<sumTime<<endl; if(sum>2&&sumTime>k){ strcpy(gang[t].x,name[head]); gang[t].num=sum; t++; } } } printf("%d\n",t); sort(gang,gang+t); for(int i=0;i<t;i++){ printf("%s %d\n",gang[i].x,gang[i].num); } return 0;}
0 0
- PAT Head of a Gang
- pat 1034 Head of a Gang
- PAT 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang
- PAT 1034. Head of a Gang
- PAT 1034. Head of a Gang
- PAT 1034 Head of a Gang 模拟
- PAT 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)
- 【PAT】1034. Head of a Gang (30)
- PAT 1034. Head of a Gang
- 【PAT】1034. Head of a Gang
- PAT A1034. Head of a Gang (30)
- pat-a1034. Head of a Gang (30)
- PAT [A1034]-Head of a Gang
- Head of a Gang
- 创业日志(七)金钱观
- Git 小白进阶整理
- [带标号无向连通图计数 容斥原理 多项式求逆 多项式求ln 模板题] BZOJ 3456 城市规划
- 设计理念要以人为本
- MySql 5.7 无法创建用户
- PAT Head of a Gang
- 埃拉托色尼筛选法
- spring boot logback.xml 文件 配置出力到linux 下的系统log
- Node实践总结6——多表查询
- 统计 最小 、最大、平均值
- 常见前端面试题及答案(下)
- iOS 面试题(2):对象内存结构中的 isa 指针是用来做什么的?
- 成为专业程序员路上用到的各种优秀资料、神器及框架
- Android ListView(一)