zoukankan      html  css  js  c++  java
  • 最小生成树

    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

    所遇皆星河
  • 相关阅读:
    七月在线爬虫班学习笔记(五)——scrapy spider的几种爬取方式
    七月在线爬虫班学习笔记(四)——相关库使用与登录问题
    七月在线爬虫班学习笔记(三)——爬虫基础知识与简易爬虫实现
    文件处理工具类
    执行脚本工具类
    LRU
    springboot 整合 memcached
    转载连接
    SpringBoot2.x中redis使用(lettuce)
    Redis 数据类型
  • 原文地址:https://www.cnblogs.com/Shallow-dream/p/13810826.html
Copyright © 2011-2022 走看看