标签:lib lines scan play out ext ram make 1.0
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 360 Accepted Submission(s): 151
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <queue> #include <cstdlib> #include <iomanip> #include <cmath> #include <ctime> #include <map> #include <set> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define pi acos(-1.0) #define ei exp(1) #define PI 3.141592653589793238462 #define ios() ios::sync_with_stdio(false) #define INF 0x3f3f3f3f #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; int f[2009]; int n,m,t; struct Node { ll u; ll v; ll w; friend bool operator<(const Node&a,const Node &b) { return a.w<b.w; } }e[15009]; int find(int x) { return f[x]==x?x:find(f[x]); } ll kruskal() { ll ans=INF,minn,maxn; int q; for(int i=0;i<m;i++) { minn=e[i].w,maxn=e[i].w; q=1; for(int k=1;k<=n;k++) f[k]=k; for(int j=i;j<m;j++) { int u=find(e[j].u); int v=find(e[j].v); if(u!=v) { maxn=max(maxn,e[j].w); if(u<v) f[u]=v; else f[v]=u; q++; if(q==n) break; } } if(q==n) ans=min(ans,maxn-minn); } if(ans==INF) return -1; return ans; } int main() { scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { scanf("%lld%lld%lld",&e[i].u,&e[i].v,&e[i].w); } sort(e,e+m); printf("%lld\n",kruskal()); } return 0; }
标签:lib lines scan play out ext ram make 1.0
原文地址:http://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7281798.html