最小生成树

Networking

kruskal

#pragma GCC optimize(2)
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
using namespace std;
const int inf=0x3f3f3f3f;
#define eps 1e-5 
#define pii pair<int,int>
#define FI first
#define SE second
#define ll long long
#define ull unsigned long long
const ll mod = 4294967296;/// 998244353;
const int mxn = 1e5 +7;
int _ , n , m , t , k ,  cnt , si , res ,ans ;
template <class T>
void rd(T &x){
	T flag = 1 ; x = 0 ; char ch = getchar() ;
	while(!isdigit(ch)) { if(ch=='-') flag = -1; ch = getchar(); }
	while(isdigit(ch)) { x = (x<<1) + (x<<3) + (ch^48); ch = getchar(); }
	x*=flag;
}
int per[mxn] , uni[mxn] ;
struct node { 
	int u,v,w;
	bool operator < (const node & a) const {
		return w<a.w;
	}
}no[mxn];
int Find(int x){
	if(x==per[x]) return x;
	return per[x] = Find(per[x]);
}
void solve()
{
	while(cin>>n&&n){
		cin>>m;
		for(int i=1;i<=n;i++) per[i] = i;
		for(int i=1;i<=m;i++)
			cin>>no[i].u>>no[i].v>>no[i].w;
		sort(no+1,no+1+m);
		ll ans = 0 ;
		for(int i=1;i<=m;i++){
			int ui = Find(no[i].u) , vi = Find(no[i].v) ;
			if(ui!=vi){
				per[ui] = vi;
				ans+=no[i].w;
			}
		}
		cout<<ans<<endl;
	}
}
int main(){ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve();return 0;}

Jungle Roads

#pragma GCC optimize(2)
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
using namespace std;
const int inf=0x3f3f3f3f;
#define eps 1e-5 
#define pii pair<int,int>
#define FI first
#define SE second
#define ll long long
#define ull unsigned long long
const ll mod = 4294967296;/// 998244353;
const int mxn = 1e5 +7;
int _ , n , m , t , k ,  cnt , si , res ,ans ;
template <class T>
void rd(T &x){
	T flag = 1 ; x = 0 ; char ch = getchar() ;
	while(!isdigit(ch)) { if(ch=='-') flag = -1; ch = getchar(); }
	while(isdigit(ch)) { x = (x<<1) + (x<<3) + (ch^48); ch = getchar(); }
	x*=flag;
}
int per[mxn] , uni[mxn] ;
struct node { 
	int u,v,w;
	bool operator < (const node & a) const {
		return w<a.w;
	}
}no[mxn];
int Find(int x){
	if(x==per[x]) return x;
	return per[x] = Find(per[x]);
}
void solve()
{
	while(cin>>n&&n){
		cnt = 0 ; ans = 0 ;
		for(int i=1;i<n;i++){
			char ch[5] , str[5] ; int m;
			cin>>ch>>m;
			for(int j=1;j<=m;j++){
				cin>>str>>k;
				no[++cnt].u = ch[0]-'A'+1 , no[cnt].v = str[0]-'A'+1 , no[cnt].w = k ;
			}
		}
		for(int i=1;i<=28;i++) per[i] = i ;
		sort(no+1,no+1+cnt);
		for(int i=1;i<=cnt;i++){
			int ui = Find(no[i].u) , vi = Find(no[i].v) ;
			if(ui!=vi) ans+=no[i].w , per[ui] = vi ;
		}
		cout<<ans<<endl;
	}
}
int main(){ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve();return 0;}

Constructing Roads

#pragma GCC optimize(2)
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
using namespace std;
const int inf=0x3f3f3f3f;
#define eps 1e-5 
#define pii pair<int,int>
#define FI first
#define SE second
#define ll long long
#define ull unsigned long long
const ll mod = 4294967296;/// 998244353;
const int mxn = 1e5 +7;
int _ , n , m , t , k ,  cnt , si , res ,ans ;
template <class T>
void rd(T &x){
	T flag = 1 ; x = 0 ; char ch = getchar() ;
	while(!isdigit(ch)) { if(ch=='-') flag = -1; ch = getchar(); }
	while(isdigit(ch)) { x = (x<<1) + (x<<3) + (ch^48); ch = getchar(); }
	x*=flag;
}
int per[mxn] , uni[mxn] , in ;
struct node { 
	int u,v,w;
	bool operator < (const node & a) const {
		return w<a.w;
	}
}no[mxn];
int Find(int x){
	if(x==per[x]) return x;
	return per[x] = Find(per[x]);
}
void solve()
{
	while(cin>>n){
		for(int i=1;i<=n;i++) per[i] = i ; 
		ans = 0 , cnt = 0 , in = 0 ;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++){
				cin>>m;
				if(j>i) no[++cnt].u = i , no[cnt].v = j , no[cnt].w = m ;
			}
		cin>>m;
		for(int i=1,u,v;i<=m;i++) {
			cin>>u>>v;
			int ui = Find(u) , vi = Find(v) ;
			if(ui!=vi) per[ui] = vi , ++in ;
		}
		sort(no+1,no+1+cnt);
		for(int i=1;i<=cnt;i++){
			int ui = Find(no[i].u) , vi = Find(no[i].v) ;
			if(ui!=vi)
				ans+=no[i].w , per[ui] = vi , ++in ;
			if(in==n-1) break;
		}
		cout<<ans<<endl;
	}
}
int main(){ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve();return 0;}

QS Network

#pragma GCC optimize(2)
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
using namespace std;
const int inf=0x3f3f3f3f;
#define eps 1e-5 
#define pii pair<int,int>
#define FI first
#define SE second
#define ll long long
#define ull unsigned long long
const ll mod = 4294967296;/// 998244353;
const int mxn = 1e6 +7;
int _ , n , m , t , k ,  cnt , si , res ,ans ;
template <class T>
void rd(T &x){
	T flag = 1 ; x = 0 ; char ch = getchar() ;
	while(!isdigit(ch)) { if(ch=='-') flag = -1; ch = getchar(); }
	while(isdigit(ch)) { x = (x<<1) + (x<<3) + (ch^48); ch = getchar(); }
	x*=flag;
}
int per[mxn] , uni[mxn] , in ;
struct node { 
	int u,v,w;
	bool operator < (const node & a) const {
		return w<a.w;
	}
}no[mxn];
int Find(int x){
	if(x==per[x]) return x;
	return per[x] = Find(per[x]);
}
void solve()
{
	for(cin>>t;t;t--){
		cin>>n;
		for(int i=1;i<=n;i++) cin>>uni[i] ;
		for(int i=1;i<=n;i++) per[i] = i ; 
		ans = 0 , cnt = 0 , in = 0 ;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++){
				cin>>m;
				if(j>i) no[++cnt].u = i , no[cnt].v = j , no[cnt].w = m + uni[i] + uni[j] ;
			}
		sort(no+1,no+1+cnt);
		for(int i=1;i<=cnt;i++){
			int ui = Find(no[i].u) , vi = Find(no[i].v) ;
			if(ui!=vi)
				ans+=no[i].w , per[ui] = vi , ++in ;
			if(in==n-1) break;
		}
		cout<<ans<<endl;
	}
}
int main(){ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve();return 0;}

计算距离套用模板↓

畅通工程再续

Truck History

Arctic Network

Highways

Agri-Net

Borg Maze

posted @ 2020-10-13 20:01  __MEET  阅读(72)  评论(0编辑  收藏  举报