邻接矩阵:

#include<stdio.h>
#include<string.h>

int map[100][100];

int main()
{
    int n, m, i, j, u, v, w, k;
    while(~scanf("%d %d", &n, &m))
    {
        memset(map, 0, sizeof(map));
        for(i=1; i<=m; i++)
        {
            scanf("%d %d %d", &u, &v, &w);
            map[u][v] = w;
            map[v][u] = w;
        }

        for(i=1; i<=n; i++)
        {
            printf("与 %d 相关联的点 :", i);
            int k = 0;
            for(j = 1; j <= m; j++)
            {
                if(k == 1)
               {
                   printf(" ");
               }
               if(map[i][j] != 0)
               {
                   printf("%d", j);
                   k = 1;
               }

            }
            printf("\n");
        }
    }
    return 0;
}

 

posted @ 2014-02-19 13:28  6bing  阅读(97)  评论(0编辑  收藏  举报