ACM2016多校联赛1A Abandoned country

题目链接//http://acm.hdu.edu.cn/contests/contest_showproblem.php?cid=704&pid=1001

Abandoned country

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
An abandoned country has  n(n100000)  villages which are numbered from 1 to  n . Since abandoned for a long time, the roads need to be re-built. There are  m(m1000000)  roads to be re-built, the length of each road is  wi(wi1000000) . Guaranteed that any two  wi  are different. The roads made all the villages connected directly or indirectly before destroyed. Every road will cost the same value of its length to rebuild. The king wants to use the minimum cost to make all the villages connected with each other directly or indirectly. After the roads are re-built, the king asks a men as messenger. The king will select any two different points as starting point or the destination with the same probability. Now the king asks you to tell him the minimum cost and the minimum expectations length the messenger will walk.
 

Input
The first line contains an integer  T(T10)  which indicates the number of test cases. 

For each test case, the first line contains two integers  n,m  indicate the number of villages and the number of roads to be re-built. Next  m  lines, each line have three number  i,j,wi , the length of a road connecting the village  i  and the village  j  is  wi .
 

Output
output the minimum cost and minimum Expectations with two decimal places. They separated by a space.
 

Sample Input
  
  
1 4 6 1 2 1 2 3 2 3 4 3 4 1 4 1 3 5 2 4 6
 

Sample Output
  
  
6 3.33
 


思路:用最小生成树先建树。然后更新权值就可以了。

看代码吧。。心累,最后一次提交刚好时间过去,然后去杭电交就A了。


代码如下:

#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long LL;
using namespace std;
const int maxn=100005;
int T,m,n,u,v,w,length=0;
int head[maxn];
bool vis[maxn];
struct node
{
  int x,value,next,F;
  friend bool operator <(const node& x,const node& y)
  {
    return x.value>y.value;
  }
}p[2*maxn*10];
vector<int>son[maxn];
LL num[maxn];
int val[maxn];
int soncount[maxn];
LL res=0;
void solve(int now)
{
  int size=son[now].size();
  if(size==0)
  {
    num[now]=n-1;
    //cout<<"res="<<res<<" ";
    res+=num[now]*val[now];
  }
  for(int i=0;i<size;i++)
  {
    int s=son[now][i];
    solve(s);
    soncount[now]+=(soncount[s]+1);
  }
  
  num[now]=(LL)(n-1-soncount[now])*(soncount[now]+1);
}
int main()
{  
  scanf("%d",&T);
  while(T--)
  {
    length=0;
    priority_queue<node>Q;
    memset(head,-1,sizeof(head));
    for(int i=0;i<=n;i++)
    {
      vis[i]=false;
      soncount[i]=0;
      num[i]=0;
      val[i]=0;
    }
    scanf("%d%d",&n,&m);
    while(m--)
    {
      scanf("%d%d%d",&u,&v,&w);
      p[length].x=v;
      p[length].value=w;
      p[length].next=head[u];
      head[u]=length;
      length++;
      p[length].x=u;
      p[length].value=w;
      p[length].next=head[v];
      head[v]=length;
      length++;
    }
    vis[1]=true;
    int count=1;
    int start=1;
    num[1]=val[1]=0;
    LL sum=0;
    for(int i=head[start];i!=-1;i=p[i].next)
    {
      p[i].F=1;
      Q.push(p[i]);
    }
    node temp,t;
    while(count<n)
    {
      temp=t;
      t=Q.top();Q.pop();
      int tminpos=t.x;
      if(vis[tminpos])continue;
      else 
      {        
        int father=t.F;
        son[father].push_back(tminpos);
        val[tminpos]=t.value;
        vis[tminpos]=true;
        count++;
        sum+=t.value;
        start=tminpos;
        for(int i=head[start];i!=-1;i=p[i].next)
        {
          if(!vis[p[i].x])
          {  
            p[i].F=start;
            Q.push(p[i]);            
          }
        }
      }
    }
    for(int i=1;i<=n;i++)
    {
      int size=son[i].size();
    }
    solve(1);
    res=0;
    for(int i=1;i<=n;i++)  
    {
      res+=(LL)num[i]*val[i];
    }
    LL allpoint=((LL)n*(LL)(n-1))/2;
    double tres=res*1.0/allpoint;
    printf("%lld ",sum);
    printf("%.2lf\n",tres);
    for(int i=1;i<=n;i++)if(son[i].size())son[i].clear();
  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值