HDU 1102 Constructing Roads
来源:互联网 发布:新媒体与网络传播 编辑:程序博客网 时间:2024/06/06 13:16
题目大意:给你n*n的一个临界矩形 每个数代表的是当前i行与j列的距离 之后给你Q个连通的道路 以下Q行为哪两个村庄连通 求最小连通的距离 也就是求最小生成树
解题思路:这道题甚至已经把临界矩形都告诉你了 比模板题更要水 两种for循环输入 连通的道路距离标记为0
普利姆算法
//Memory: 252 KB //Time: 16 MS #include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <map>#include <cmath>#include <queue>#include <string>#include <vector>#include <set>using namespace std;#define ll long long#define sc(x) scanf("%d",&x)#define dsc(x,y) scanf("%d%d",&x,&y)#define pr(x) printf("%d\n",x)#define FOR(i,n,o) for(int i=o;i<=n;i++)#define lcr(a,b) memset(a,b,sizeof(a))#define Inf 1<<29#define N 104int n,q,a,b;int mp[N][N],vis[N],dis[N],cis[N][N];int pri(){ int sum=0; FOR(i,n,1) { dis[i]=mp[1][i]; } vis[1]=1; FOR(i,n-1,1) { int to=-1; int minn=Inf; FOR(j,n,1) { if(!vis[j]&&dis[j]<minn) { to=j; minn=dis[j]; } } if(to==-1) return 0; vis[to]=1; sum+=minn; FOR(j,n,1) { if(!vis[j]) dis[j]=min(dis[j],mp[to][j]); } } return sum;}int main(){ while(~sc(n)) { lcr(vis,0); lcr(cis,0); FOR(i,n,1) { FOR(j,n,1) { mp[i][j]=Inf; } } FOR(i,n,1) { FOR(j,n,1) { int v; scanf("%d",&v); mp[i][j]=v; } } sc(q); while(q--) { dsc(a,b); mp[a][b]=0; mp[b][a]=0; } pr(pri()); } return 0;}
克鲁斯卡尔算法
//Memory: 332 //Time: 79 #include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <map>#include <cmath>#include <queue>#include <string>#include <vector>#include <set>using namespace std;#define ll long long#define sc(x) scanf("%d",&x)#define dsc(x,y) scanf("%d%d",&x,&y)#define pr(x) printf("%d\n",x)#define FOR(i,n,o) for(int i=o;i<=n;i++)#define lcr(a,b) memset(a,b,sizeof(a))#define Inf 1<<29#define N 105int n,v,p[N],mp[N][N];struct node{ int x; int y; int l;}q[N*N];int Find(int x){ if(x!=p[x]) { p[x]=Find(p[x]); } return p[x];}int cmp(node a,node b){ return a.l<b.l;}int Kruskal(int l){ int sum=0; int to=0; FOR(i,l,1) { int xx=Find(q[i].x); int yy=Find(q[i].y); if(xx!=yy) { p[yy]=xx; sum+=q[i].l; to++; if(to==n-1) return sum; } } return sum;}int main(){ while(~sc(n)) { int k=1; FOR(i,n,1) { p[i]=i; FOR(j,n,1) { sc(v); mp[i][j]=v; } } int m; sc(m); while(m--) { int a,b; scanf("%d %d",&a,&b); mp[a][b]=0; mp[b][a]=0; } FOR(i,n,1) { FOR(j,n,1) { q[k].x=i; q[k].y=j; q[k++].l=mp[i][j]; } } sort(q+1,q+k,cmp); int ans=Kruskal(k); pr(ans); } return 0;}
END!!!!!!!!!!!!!!!!!!!
2 0
- HDU 1102 Constructing Roads
- HDU 1102 Constructing Roads
- Hdu-1102 Constructing Roads
- Hdu-1102 Constructing Roads
- hdu 1102 Constructing Roads
- HDU 1102 Constructing Roads
- hdu 1102 Constructing Roads
- hdu 1102 Constructing Roads
- HDU 1102 Constructing Roads
- HDU 1102 Constructing Roads
- HDU 1102 Constructing Roads
- hdu 1102 Constructing Roads
- Hdu 1102 - Constructing Roads
- hdu 1102 Constructing Roads
- HDU 1102 Constructing Roads
- HDU 1102 Constructing Roads
- HDU 1102 Constructing Roads
- HDU 1102 Constructing Roads
- 机器学习实战ByMatlab(四)二分K-means算法
- 问题 O 芯片测试
- POJ 2752 Seek the Name, Seek the Fame (KMP)
- session与cookie的区别以及配置
- PTA5-1 任务调度的合理性
- HDU 1102 Constructing Roads
- 问题 N 报时助手
- 问题 M 十六进制转十进制
- noip提高组写题心得——贪心4例
- Linux驱动开发之LDD3中第三章scull注释详解
- STL的空间配置器
- 问题 L 校门外的树
- 简单理解AOP(面向切面编程)
- kmp算法(字符串匹配 next应用 hdu题目集合)