本文主要是介绍17级计科ACM程序设计(下),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最小生成树(并查集)
hdu1233 还是畅通工程 hdu1863 畅通工程 hdu1879 继续畅通工程 hdu1232 畅通工程
以hdu1878继续畅通工程为例
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct F {int x;int y;int power;int flag;
}f[10010];
int city[110],sum,t;
bool cmp(F a,F b)
{return a.power<b.power;
}
int findset(int x)
{int r=x;while(city[r]!=r)r=city[r];return r;
}
void unionxy(int x,int y,int power)
{int fx,fy;fx=findset(x);fy=findset(y);if(fx!=fy){city[fx]=fy;sum+=power;}
}
int main()
{int n,m;while(~scanf("%d",&n)&&n){sum=0;m=n*(n-1)/2;for(int i=1;i<=n;i++){city[i]=i;}for(int i=1;i<=m;i++){scanf("%d %d %d %d",&f[i].x,&f[i].y,&f[i].power,&f[i].flag);if(f[i].flag==1){f[i].power=0;}}sort(f+1,f+m+1,cmp);for(int i=1;i<=m;i++){unionxy(f[i].x,f[i].y,f[i].power);}printf("%d\n",sum);}return 0;
}
topsort(拓扑排序)
poj4084 so easy
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<string>
using namespace std;
int mp[1010][1010],indegree[1010],pur[1010];
int main()
{int n,m,a,b;while(cin>>n>>m){memset(mp,0,sizeof(mp));memset(indegree,0,sizeof(indegree));memset(pur,0,sizeof(pur));for(int i=1; i<=m; i++){cin>>a>>b;if(mp[a][b]==0){mp[a][b]=1;indegree[b]++;}}int k=1;for(int i=1; i<=n; i++){for(int j=1; j<=n; j++){if(indegree[j]==0){indegree[j]--;pur[k++]=j;for(int x=1; x<=n; x++){if(mp[j][x])indegree[x]--;}break;}}}k--;for(int i=1; i<=k; i++){if(i!=k)cout<<"v"<<pur[i]<<" ";elsecout<<"v"<<pur[i]<<endl;}}return 0;
}
这篇关于17级计科ACM程序设计(下)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!