hdu 1102 Constructing Roads +1879 继续畅通工程(MST)
来源:互联网 发布:淘宝买家退款率50 编辑:程序博客网 时间:2024/06/03 11:57
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102
思路:
过该路已经建好,直接把边权值修改为0即可。
这题用Prim和kruskal算法都能过。
hdu 1102:
#include<stdio.h>#include<iostream>#include<string>#include<string.h>#include<algorithm>#include<vector>#include<time.h>#include<queue>#include<stack>#include<iterator>#include<math.h>#include<stdlib.h>#include<limits.h>#include<memory.h>//#define ONLINE_JUDGE#define eps 1e-8#define INF 0x7fffffff#define FOR(i,a) for((i)=0;i<(a);(i)++) //[i,a);#define MEM(a) (memset((a),0,sizeof(a)))#define sfs(a) scanf("%s",a)#define sf(a) scanf("%d",&a)#define sfI(a) scanf("%I64d",&a)#define pf(a) printf("%d\n",a)#define pfI(a) printf("%I64d\n",a)#define pfs(a) printf("%s\n",a)#define sfd(a,b) scanf("%d%d",&a,&b)#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)#define for1(i,a,b) for(int i=(a);i<b;i++)#define for2(i,a,b) for(int i=(a);i<=b;i++)#define for3(i,a,b)for(int i=(b);i>=a;i--)#define MEM1(a) memset(a,0,sizeof(a))#define MEM2(a) memset(a,-1,sizeof(a))#define LL __int64const double PI = acos(-1.0);template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }template<class T> inline T Min(T a, T b) { return a < b ? a : b; }template<class T> inline T Max(T a, T b) { return a > b ? a : b; }using namespace std;template<class T>T Mint(T a, T b, T c) { if (a>b) { if (c>b) return b; return c; } if (c>a) return a; return c;}template<class T>T Maxt(T a, T b, T c) { if (a>b) { if (c>a) return c; return a; } else if (c > b) return c; return b;}const int maxn=105;int T,n,m;int a,b,c,pos;int ans,cnt;int map[maxn][maxn],low[maxn];bool vis[maxn];int Prim(){ MEM1(vis); int ans=0; vis[1]=1;pos=1; for(int i=1;i<=n;i++){ if(i!=pos) low[i]=map[pos][i]; } for(int i=1;i<n;i++){ int min=INT_MAX; for(int j=1;j<=n;j++){ if(!vis[j]&low[j]<min){ min=low[j]; pos=j; } } vis[pos]=1; for(int j=1;j<=n;j++){ if(!vis[j]&&low[j]>map[pos][j]) low[j]=map[pos][j]; } ans+=min; } return ans;}int main(){#ifndef ONLINE_JUDGE freopen("test.in","r",stdin); freopen("test.out","w",stdout);#endif while(~sf(n)){ for2(i,1,n){ for2(j,1,n){ sf(map[i][j]); } } sf(m); for1(i,0,m){ sfd(a,b); map[a][b]=map[b][a]=0; } ans=Prim(); pf(ans); } return 0;}
hdu 1879:
#include <limits.h>#include <math.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <algorithm>#include <iostream>#include <iterator>#include <sstream>#include <queue>#include <stack>#include <string>#include <vector>#include <set>//#define ONLINE_JUDGE#define eps 1e-6#define INF 0x7fffffff //INT_MAX#define inf 0x3f3f3f3f //int??????????????????#define FOR(i,a) for((i)=0;i<(a);(i)++) //[i,a);#define MEM(a) (memset((a),0,sizeof(a)))#define sfs(a) scanf("%s",a)#define sf(a) scanf("%d",&a)#define sfI(a) scanf("%I64d",&a)#define pf(a) printf("%d\n",a)#define pfI(a) printf("%I64d\n",a)#define pfs(a) printf("%s\n",a)#define sfd(a,b) scanf("%d%d",&a,&b)#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)#define for1(i,a,b) for(int i=(a);i<b;i++)#define for2(i,a,b) for(int i=(a);i<=b;i++)#define for3(i,a,b)for(int i=(b);i>=a;i--)#define MEM1(a) memset(a,0,sizeof(a))#define MEM2(a) memset(a,-1,sizeof(a))#define MEM3(a) memset(a,0x3f,sizeof(a))#define MEMS(a) memset(a,'\0',sizeof(a))#define LL __int64const double PI = acos(-1.0);template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }template<class T> inline T Min(T a, T b) { return a < b ? a : b; }template<class T> inline T Max(T a, T b) { return a > b ? a : b; }using namespace std;template<class T>T Mint(T a, T b, T c) { if (a>b) { if (c>b) return b; return c; } if (c>a) return a; return c;}template<class T>T Maxt(T a, T b, T c) { if (a>b) { if (c>a) return c; return a; } else if (c > b) return c; return b;}const int maxn = 105;int T, n, m;int map[maxn][maxn];bool vis[maxn];//标记数组int low[maxn];//i点到邻近点(未标记)的最小距离int a,b,c,d;int Prim(){ int ans=0,pos,min; MEM1(vis); //从某点开始,分别标记和记录该点 vis[1]=1;pos=1; //第一次给low[]赋值 for2(i,1,n){ if(i!=pos) low[i]=map[pos][i]; } for1(i,1,n){//找最小值并记录位置 min=INT_MAX; for2(j,1,n){ if(!vis[j]&&min>low[j]){ min=low[j]; pos=j; } } ans+=min; vis[pos]=1; for2(j,1,n){ if(!vis[j]&&low[j]>map[pos][j]) low[j]=map[pos][j]; } } return ans;}int main() {#ifndef ONLINE_JUDGE freopen("test.in","r",stdin); freopen("test.out","w",stdout);#endif while(~sf(n)&&n){ int num=n*(n-1)/2; for2(i,1,num){ scanf("%d%d%d%d",&a,&b,&c,&d); if(d==0) map[a][b]=map[b][a]=c; else map[a][b]=map[b][a]=0; } int ans=Prim(); pf(ans); } return 0;}
0 0
- hdu 1102 Constructing Roads +1879 继续畅通工程(MST)
- HDU -- 1789 继续畅通工程(mst)
- HDU 1102 && POJ 2421 Constructing Roads (经典MST~Prim)
- [MST]POJ 2421&HDU 1102 Constructing Roads
- 继续畅通工程(MST)
- HDU:1879 继续畅通工程(kruskal)
- HDU 1879 继续畅通工程(Kruskra)
- hdu 1879 继续畅通工程(Kruskal)
- HDU 1102/POJ 2421 Constructing Roads(MST&Prim优化)
- HDU 1879 继续畅通工程
- 继续畅通工程 hdu 1879
- hdu 1879 继续畅通工程
- HDU 1879 继续畅通工程
- hdu 1879 继续畅通工程
- hdu 1879 继续畅通工程
- HDU 1879 继续畅通工程
- hdu 1879 继续畅通工程
- hdu 1879 继续畅通工程
- 经验之谈—让你看明白block
- 读书笔记
- cocos2dx
- eggs(递归)
- SQL Server 数据库帐号密码生成
- hdu 1102 Constructing Roads +1879 继续畅通工程(MST)
- SQL Server 数据库帐号密码安全设计
- Android中手机震动vibrate的实现
- SQL Server 2005 控制用户权限访问表
- Nullability and Objective-C
- java多线程网络服务器端向客户端发送文件
- 做个备忘(linux重装重要文件)
- App性能优化浅谈
- jQuery中ready与load事件的区别