poj 1751 highways

用prim能过,用ku...超时


ku...

#include<iostream>
using namespace std;

struct town{
       int x;
       int y;       
}town[800];

struct dist{
       int from;
       int to;
       int dis;
       int vis;
}dist[320000];

int cmp(const void *a, const void *b)
{
    return (*(struct dist *)a).dis > (*(struct dist *)b).dis ? 1 : -1;
}

int fa[800];
int find(int x)
{
    if(x == fa[x])
         return x;
    fa[x] = find(fa[x]);
    return fa[x];
}

int main()
{
    int N, M, i;
    cin>>N;
    for(i=0;i<N;i++)
    {
          cin>>town[i].x>>town[i].y;
    }
    
    cin>>M;
    for(i=0;i<M;i++)
    {
          cin>>dist[i].from>>dist[i].to;
          dist[i].dis = 0;
          dist[i].vis = 0;
    }
    
       int j, k;
       i = M;
        for(j=0;j<N;j++)
        {
             for(k=j+1;k<N;k++)
             {
                  
     
                  dist[i].vis = 1;
                  dist[i].from = j+1;
                  dist[i].to = k + 1;
                  dist[i].dis = (town[j].x-town[k].x)*(town[j].x-town[k].x) + (town[j].y-town[k].y)*(town[j].y-town[k].y);
                  i++;
             }
        }
    
    
    int num = M+N*(N-1)/2;
    qsort(dist, num, sizeof(dist[0]), cmp);
    
    
    for(i=1;i<=N;i++)
        fa[i]=i;
    
    int count = 0;
    for(i=0;i<num;i++)
    {
        if(find(dist[i].from) != find(dist[i].to))
        {
            fa[find(dist[i].from)] = find(dist[i].to);
            count++;
            if(dist[i].vis)
                   cout<<dist[i].from<<" "<<dist[i].to<<endl;
            
            if(count == N-1)
                break; 
        }
    }
    
    system("pause");
    return 0;
}

prim

#include<iostream>
#include<climits>
#include<cstring>
using namespace std;

struct node{
       int x, y;
}node[755];

int maxEdge=INT_MAX;
int flag[755];
int pre[755];
int dist[755];

int map[755][755];
int main()
{
    int N, M;
    cin>>N;
    for(int i=1;i<=N;i++)
    {
            cin>>node[i].x>>node[i].y;
    }
    memset(map, 0, sizeof(map));
    for(int i=1;i<=N;i++)
    {
            for(int j=1;j<=N;j++)
            {
                    map[i][j]=(node[i].x-node[j].x)*(node[i].x-node[j].x)+(node[i].y-node[j].y)*(node[i].y-node[j].y);
            }
    }
    for(int i=0;i<=N;i++)
    {
            flag[i]=0;
            dist[i]=maxEdge;
            pre[i]=1;
    }
    
    cin>>M;
    for(int i=1;i<=M;i++)
    {
         int a, b;
         cin>>a>>b;
         pre[b] = a;
         map[a][b]=0;
         map[b][a]=0;   
    }
    
   
    flag[1]=1;
    dist[1]=maxEdge;  
    for(int j=2;j<=N;j++)
    {
         dist[j]=map[1][j];
    }
      
    int next,p=1; 
    for(int i=1;i<N;i++)
    {  
        int min=maxEdge; 
        
        for(int j=1;j<=N;j++)
        {
            if(dist[j]<min)
            {
                min=dist[j];
                next = j;
            }                  
        }
         p=pre[next];
         flag[next]=1;
         dist[next]=maxEdge;
         pre[next]=p;
         if(map[pre[next]][next]  != 0)
         {
              cout<<pre[next]<<" "<<next<<endl;
         }
        for(int j=1;j<=N;j++)
        {
                if((map[next][j] < dist[j])&& flag[j]==0)
                {
                    dist[j]=map[next][j];
                    pre[j]=next;
                }
        }           
           
    }
    system("pause");
    return 0;
}



posted on 2012-04-19 20:37  java课程设计例子  阅读(297)  评论(0编辑  收藏  举报