- Kerry 的电缆网络
- 2013-03-11 21:30:03 @
#include<iostream>
#include<fstream>
#include <algorithm>
#include <iomanip>
using namespace std;
const int mx=100000;
int u[mx],v[mx],r[mx],p[mx],n,m;
double w[mx],s,total;
int cmp(const int i,const int j)
{
return w[i]<w[j];
}
int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);
}
double kruskal()
{
double ans=0;
for (int i=1;i<=n;i++) p[i]=i;
for (int i=0;i<m;i++) r[i]=i;
sort(r,r+m,cmp);
for (int i=0;i<m;i++)
{
int e=r[i];int x=find(u[e]);int y=find(v[e]);
if(x!=y){ans+=w[e];p[x]=y;}
}
return(ans);
}
int main()
{
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
ios::sync_with_stdio(false);
cin>>s;
cin>>n;
m=-1;
while (!cin.eof())
{
m++;
cin>>u[m]>>v[m]>>w[m];
}
total=kruskal();
if (total>s)cout<<"Impossible"<<endl;
else cout<<"Need "<<fixed<<setprecision(2)<<total<<" miles of cable"<<endl;
}
测试数据 #0: Accepted, time = 671 ms, mem = 2572 KiB, score = 10
测试数据 #1: Accepted, time = 296 ms, mem = 2572 KiB, score = 10
测试数据 #2: Accepted, time = 1375 ms, mem = 2572 KiB, score = 10
测试数据 #3: Accepted, time = 984 ms, mem = 2572 KiB, score = 10
测试数据 #4: WrongAnswer, time = 1453 ms, mem = 2572 KiB, score = 0
测试数据 #5: WrongAnswer, time = 1437 ms, mem = 2572 KiB, score = 0
测试数据 #6: WrongAnswer, time = 1453 ms, mem = 2572 KiB, score = 0
测试数据 #7: Accepted, time = 1453 ms, mem = 2572 KiB, score = 10
测试数据 #8: Accepted, time = 1437 ms, mem = 2572 KiB, score = 10
测试数据 #9: WrongAnswer, time = 0 ms, mem = 2572 KiB, score = 0
Summary: WrongAnswer, time = 10559 ms, mem = 2572 KiB, score = 60