pku 1751

  1. #include <stdio.h>
  2. #define INF 200000000
  3. #define MAX 751
  4. int path[MAX];
  5. int matrix[MAX][MAX];
  6. int main()
  7. {
  8.     int x[MAX],y[MAX];
  9.     int N,M;
  10.     int p1,p2;
  11.     int temp1,temp2;
  12.     int i,j;
  13.     int cost[MAX];
  14.     int min;
  15.     int u;
  16.     scanf("%d",&N);
  17.     for(i=1;i<=N;i++)
  18.         scanf("%d%d",&x[i],&y[i]);
  19.     for(i=1;i<=N;i++)
  20.         for(j=i;j<=N;j++)
  21.             if(i==j)
  22.                 matrix[i][i]=INF;
  23.             else
  24.             {
  25.                 temp1=x[i]-x[j];
  26.                 temp2=y[i]-y[j];
  27.                 matrix[j][i]=matrix[i][j]=temp1*temp1+temp2*temp2;
  28.             }
  29.     scanf("%d",&M);
  30.     for(i=1;i<=M;i++)
  31.     {
  32.         scanf("%d%d",&p1,&p2);
  33.         matrix[p1][p2]=matrix[p2][p1]=0;
  34.     }
  35.     
  36.     for(i=1;i<=N;i++)
  37.     {
  38.         cost[i]=matrix[1][i];
  39.         path[i]=1;
  40.     }
  41.     cost[1]=-1;
  42.     for(i=1;i<=N-1;i++)
  43.     {
  44.         min=INF;
  45.         for(j=1;j<=N;j++)
  46.             if(cost[j]!=-1 && cost[j]<min)
  47.             {
  48.                 u=j;
  49.                 min=cost[j];
  50.             }
  51.         if(min>0)
  52.             printf("%d %d/n",path[u],u);
  53.         cost[u]=-1;
  54.         for(j=1;j<=N;j++)
  55.             if(cost[j]!=-1 && cost[j]>matrix[u][j])
  56.             {
  57.                 path[j]=u;
  58.                 cost[j]=matrix[u][j];
  59.             }
  60.     }
  61.     return 0;
  62. }
  63. /*18 4543344(5) xiaofengsheng 2404K 79MS C++ 1028B 2009-01-09 15:38:56
  64. 当边稠密时最好用prim,而不宜用Kruskal
  65. (这里是已有添加好边的minimum spanning tree problem)
  66. 经过几次提交发现:
  67.     1.typedef struct point
  68.     {
  69.         int x;
  70.         int y;
  71.     }point;
  72.     用结构体稍微比数组 int x[MAX],y[MAX]; 慢,不过结构体结构清晰;
  73.     2.if(i==j)
  74.             matrix[i][i]=INF;
  75.     开始时处理的不好,
  76.     for(i=1;i<=N;i++)
  77.         for(j=1;j<=N;j++)
  78.             matrix[i][j]=INF;
  79. 另外,多亏suikay代码的有益启发!thanks...
  80. */
    1. #include <stdio.h>
    2. #define INF 200000000
    3. #define MAX 751
    4. typedef struct point
    5. {
    6.     int x;
    7.     int y;
    8. }point;
    9. point p[MAX];
    10. int path[MAX];
    11. int matrix[MAX][MAX];
    12. void main()
    13. {
    14.     int N,M;
    15.     int p1,p2;
    16.     int temp1,temp2;
    17.     int i,j;
    18.     int cost[MAX];
    19.     int min;
    20.     int u;
    21.     scanf("%d",&N);
    22.     for(i=1;i<=MAX;i++)
    23.         for(j=1;j<=MAX;j++)
    24.             matrix[i][j]=INF;
    25.     for(i=1;i<=N;i++)
    26.         scanf("%d%d",&p[i].x,&p[i].y);
    27.     for(i=1;i<=N;i++)
    28.         for(j=i+1;j<=N;j++)
    29.         {
    30.             temp1=p[i].x-p[j].x;
    31.             temp2=p[i].y-p[j].y;
    32.             matrix[j][i]=matrix[i][j]=temp1*temp1+temp2*temp2;
    33.         }
    34.     scanf("%d",&M);
    35.     for(i=1;i<=M;i++)
    36.     {
    37.         scanf("%d%d",&p1,&p2);
    38.         matrix[p1][p2]=matrix[p2][p1]=0;
    39.     }
    40.     
    41.     //prim
    42.     for(i=1;i<=N;i++)
    43.     {
    44.         cost[i]=matrix[1][i];
    45.         path[i]=1;
    46.     }
    47.     cost[1]=-1;
    48.     for(i=1;i<=N-1;i++)
    49.     {
    50.         min=INF;
    51.         for(j=1;j<=N;j++)
    52.             if(cost[j]!=-1 && cost[j]<min)
    53.             {
    54.                 u=j;
    55.                 min=cost[j];
    56.             }
    57.         if(min>0)
    58.             printf("%d %d/n",path[u],u);
    59.         cost[u]=-1;
    60.         for(j=1;j<=N;j++)
    61.             if(cost[j]!=-1 && cost[j]>matrix[u][j])
    62.             {
    63.                 path[j]=u;
    64.                 cost[j]=matrix[u][j];
    65.             }
    66.     }
    67. }
    68. //最开始的code
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值