1010 道路建设 最小生成树板子
链接:https://ac.nowcoder.com/acm/contest/26077/1010
来源:牛客网
*/void AC(){ //// _oo0oo_// o8888888o// 88" . "88// (| -_- |)// 0\ = /0// ___/`---'\___// .' \\| |// '.// / \\||| : |||// \// / _||||| -:- |||||- \// | | \\\ - /// | |// | \_| ''\---/'' |_/ |// \ .-\__ '-' ___/-. /// ___'. .' /--.--\ `. .'___// ."" '< `.___\_<|>_/___.' >' "".// | | : `- \`.;`\ _ /`;.`/ - ` : | |// \ \ `_. \_ __\ /__ _/ .-` / /// =====`-.____`.___ \_____/___.-`___.-'=====// 佛祖保佑, 永无bug;}
//-------------------------代码----------------------------
//#define int llconst int N = 1e5+10;int n,m,c;int p[N];int find (int x) { return x == p[x] ? x:p[x] = find(p[x]);}struct node { int a,b,h; bool operator<(const node &x) const { return h < x.h; }}g[N];void solve(){cin>>n>>m; fo(i,1,m) p[i] = i; fo(i,1,n) { int a,b,h;cin>>a>>b>>h; g[i] = {a,b,h}; } sort(g+1,g+1+n);ll sum = 0; fo(i,1,n) { int a = find(g[i].a),b = find(g[i].b); if(p[a] != p[b]) p[a] = b,sum += g[i].h; } cout<<(sum <= c? "Yes":"No")<<endl;}void main_init() {}signed main(){AC();clapping();TLE;cout<<fixed<<setprecision(12);main_init(); while(cin>>c)// while(cin>>n>>m,n,m)//int t;cin>>t;while(t -- )solve();//{solve(); }return 0;}
/*样例区
*/
//------------------------------------------------------------
来源:牛客网
题目描述
随着如今社会的不断变化,交通问题也变得越来越重要,所以市长决定建设一些公路来方便各个城市之间的贸易和交易。虽然市长的想法很好,但是他也遇到了一般人也经常头疼的问题,那就是手头的经费有限……在规划过程中,设计师们已经预算出部分城市之间建设公路的经费需求。现在市长想知道,它能不能将他的m个城市在有限的经费内实现公路交通。如果可以的话,输出Yes,否则输出No(两个城市不一定要直接的公路相连,间接公路到达也可以。)
输入描述:
测试输入包含多条测试数据
每个测试数据的第1行分别给出可用的经费c(<1000000),道路数目n(n<10000),以及城市数目m(<100)。
接下来的n行给出建立公路的成本信息,每行给出三个整数,分别是相连的两个城市v1、v2(0<v1,v2<=m)以及建设公路所需的成本h(h<100)。
输出描述:
对每个测试用例,输出Yes或No。
备注:
两个城市之间可能存在多条线路
分析
板子题目
//-------------------------代码---------------------------- //#define int ll const int N = 1e5+10; int n,m,c; int p[N]; int find (int x) { return x == p[x] ? x:p[x] = find(p[x]); } struct node { int a,b,h; bool operator<(const node &x) const { return h < x.h; } }g[N]; void solve() { cin>>n>>m; fo(i,1,m) p[i] = i; fo(i,1,n) { int a,b,h;cin>>a>>b>>h; g[i] = {a,b,h}; } sort(g+1,g+1+n);ll sum = 0; fo(i,1,n) { int a = find(g[i].a),b = find(g[i].b); if(p[a] != p[b]) p[a] = b,sum += g[i].h; } cout<<(sum <= c? "Yes":"No")<<endl; } void main_init() {} signed main(){ AC();clapping();TLE; cout<<fixed<<setprecision(12); main_init(); while(cin>>c) // while(cin>>n>>m,n,m) // int t;cin>>t;while(t -- ) solve(); // {solve(); } return 0; } /*样例区 */ //------------------------------------------------------------
#include<bits/stdc++.h>#define TLE ios::sync_with_stdio(0),cin.tie(0)#define endl "\n"#define FILE "a"#define pb push_back#define gg exit(0);#define rt return;#define bd cout<<"debug"<<endl;#define db(x) cout<<#x<<':'<<x<<endl;#define dbb(i,a) cout<<#i<<':'<<i<<' '<<#a<<':'<<a<<' '<<endl;#define dbbb(i,a,b) cout<<#i<<':'<<i<<' '<<#a<<':'<<a<<' '<<#b<<':'<<b<<endl;#define TIME cout<<"RuningTime: "<<clock()<<"ms\n";#define YES cout<<"YES"<<endl;#define Yes cout<<"Yes"<<endl;#define NO cout<<"NO"<<endl;#define No cout<<"No"<<endl;#define None cout<<-1<<endl;#define el cout<<endl;#define x first#define y second#define V vector#define fo(i,j,n) for(int i = j;i<=n;i++)#define of(i,n,j) for(int i = n;i>=j;i--)#define fe(i,u) for(int i = h[u];~i;i=ne[i])#define all(a) a.begin(),a.end()#define alll(a) a.begin()+1,a.end()#define ms(a,b) memset(a, b, sizeof(a));#define tr_len(u) (tr[u].r - tr[u].l + 1)#define tr_mid (tr[u].l + tr[u].r >> 1)#define ul (u<<1)#define ur (u<<1|1)#define lowbit(x) (x&-x)#define gcd(a,b) __gcd(a,b)#define CLAP 11243using namespace std;void clapping() {#if CLAP == 1srand(time(NULL)+rand());freopen("a.in","r",stdin);freopen("a.out","w",stdout);//freopen("a.in","w",stdout);#endif}template<class T>inline void read(T &res) { char c;T flag = 1; while((c = getchar()) < '0' || c > '9') if(c == '-') flag = -1;res = c - '0'; while((c=getchar())>='0'&&c<='9')res=(res<<1)+(res<<3)+(c^48);res*=flag;}typedef pair<int,int> pii;typedef pair<long,long>pll;typedef long long ll;const int inf = 0x3f3f3f3f;const ll INF = 0x3f3f3f3f3f3f3f3fll;const double eps = 1e-8;int dy[] = {1,0,-1,0,1,1,-1,-1};int dx[] = {0,1,0,-1,1,-1,1,-1};template<class T> T qmi(T a,T b,T p) {T res = 1;for(;b;b>>=1,a=1ll*a*a%p)if(b&1)res = 1ll*res*a%p;return res;}template<class T> T exgcd(T a,T b,T &x,T &y) {if(b == 0) {x = 1;y = 0;return a;}ll d = gcd_ed(b,a%b,y,x);y = y - a / b * x;return d;}template<class T> T qmul(T a,T b,T p) {T res = 0;for(;b;b >>= 1,a = (a + a) % p) {res = (res + a) % p;}return res;}/*文档区
*/void AC(){ //// _oo0oo_// o8888888o// 88" . "88// (| -_- |)// 0\ = /0// ___/`---'\___// .' \\| |// '.// / \\||| : |||// \// / _||||| -:- |||||- \// | | \\\ - /// | |// | \_| ''\---/'' |_/ |// \ .-\__ '-' ___/-. /// ___'. .' /--.--\ `. .'___// ."" '< `.___\_<|>_/___.' >' "".// | | : `- \`.;`\ _ /`;.`/ - ` : | |// \ \ `_. \_ __\ /__ _/ .-` / /// =====`-.____`.___ \_____/___.-`___.-'=====// 佛祖保佑, 永无bug;}
//-------------------------代码----------------------------
//#define int llconst int N = 1e5+10;int n,m,c;int p[N];int find (int x) { return x == p[x] ? x:p[x] = find(p[x]);}struct node { int a,b,h; bool operator<(const node &x) const { return h < x.h; }}g[N];void solve(){cin>>n>>m; fo(i,1,m) p[i] = i; fo(i,1,n) { int a,b,h;cin>>a>>b>>h; g[i] = {a,b,h}; } sort(g+1,g+1+n);ll sum = 0; fo(i,1,n) { int a = find(g[i].a),b = find(g[i].b); if(p[a] != p[b]) p[a] = b,sum += g[i].h; } cout<<(sum <= c? "Yes":"No")<<endl;}void main_init() {}signed main(){AC();clapping();TLE;cout<<fixed<<setprecision(12);main_init(); while(cin>>c)// while(cin>>n>>m,n,m)//int t;cin>>t;while(t -- )solve();//{solve(); }return 0;}
/*样例区
*/
//------------------------------------------------------------