记录编号 118284 评测结果 AAAAAAAAAA
题目名称 [USACO Dec07] 建造路径 最终得分 100
用户昵称 GravatarHouJikan 是否通过 通过
代码语言 C++ 运行时间 0.116 s
提交时间 2014-09-05 21:47:42 内存使用 8.12 MiB
显示代码纯文本
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <ctime>
#include <iterator>
#include <functional>
#define pritnf printf
#define scafn scanf
#define For(i,j,k) for(int i=(j);i<=(k);(i)++)
using namespace std;
typedef long long LL;
typedef unsigned int Uint; 
#define Ldouble double
const int INF=0x7ffffff;
//==============struct declaration==============
struct points
{
  int x,y;
};
struct node
{
  int to;
  double dist;
  node (int to=0,double dist=0):to(to),dist(dist){}
  bool operator <(const node &rhs) const
  {
    return dist>rhs.dist;
  }
};
//==============var declaration=================
const int MAXN=1010;
points point[MAXN];
int n,m,s,e;
bool vis[MAXN];
Ldouble dist[MAXN][MAXN];
Ldouble d[MAXN];
//==============function declaration============
Ldouble dis(int a,int b);
void Prim();
//==============main code=======================
int main()
{  
  string FileName="roads";//程序名 
  string FloderName="COGS";//文件夹名 
  freopen((FileName+".in").c_str(),"r",stdin);
  freopen((FileName+".out").c_str(),"w",stdout);
#ifdef DEBUG  
  system(("cp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\standard.cpp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\submit.txt").c_str());
  clock_t Start_Time=clock();
#endif      
   scanf("%d%d",&n,&m);
   memset(dist,0,sizeof(dist));
   For(i,1,n)
   {
     scanf("%d %d",&point[i].x,&point[i].y);
     For(j,1,i-1)
       dist[i][j]=dist[j][i]=dis(i,j);
   }
   For(i,1,m)
   {
     scanf("%d%d",&s,&e);
     dist[s][e]=dist[e][s]=0;
   }
   Prim();
#ifdef DEBUG  
  clock_t End_Time=clock();
  cout<<endl<<endl<<"Time Used: "<<(End_Time-Start_Time)/CLOCKS_PER_SEC<<endl;
#endif    
  return 0;
}
//================fuction code====================
Ldouble dis(int i,int j)
{
  Ldouble xx=point[i].x-point[j].x;
  Ldouble yy=point[i].y-point[j].y;
  return sqrt(xx*xx+yy*yy); 
}
void Prim()
{
  memset(vis,false,sizeof(vis));
  For(i,1,n)
    d[i]=INF;
  d[1]=0;
  int done=0;
  priority_queue <node> Q;
  Q.push(node(1,0));
  Ldouble sum=0; 
  while (done<n)
  {
    node now=Q.top();Q.pop();
    if (vis[now.to]) continue;
    vis[now.to]=true;
    sum+=now.dist;
    For(i,1,n)
    {
      if (vis[i]) continue;
      if (d[i]>dist[now.to][i])
      {
        d[i]=dist[now.to][i];
        Q.push(node(i,d[i]));
      }
    }
    done++;
  }
  pritnf("%.2lf\n",sum);
}