• POJ 1847 dijstra算法


    POJ 无限循环CE中。感觉是读题难。然后就可以建图上模板了。

    附个人代码:

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #define maxn 0x1f1f1f1f
    #define size 210
    using namespace std;

    int low[size];
    bool used[size];
    int map[size][size];
    int n, a, b;

    void init()
    {
        for (int i=0; i<=n; ++i)
        {
            for (int j=0; j<=n; ++j)
            map[i][j] = maxn;
        }
        memset(used, 0, sizeof(used));
    }

    void dijkstra(int aa)
    {
        int i, j, k;
        for (i=0; i<n; ++i)
        low[i] = map[aa][i];
        low[aa] = 0;
        used[aa] = true;
        for (i=0; i<n; ++i)
        {
            int min = maxn;
            int temp;
            for (j=0; j<n; ++j)
            {
                if (used[j] == 0 && low[j] < min)
                {
                    min = low[j];
                    temp = j;
                }
            }
            if (min == maxn) break;
            used[j] = 1;
            for (j=0; j<n; ++j)
            {
                if (used[j] == 0 && low[j] > low[temp] + map[temp][j])
                {
                    low[j] = low[temp] + map[temp][j];
                }
            }
        }
    }

    int main()
    {
        while(~scanf("%d%d%d", &n, &a, &b))
        {
            init();
            a--;
            b--;
            for (int i=0; i<n; ++i)  //第i个节点的信息
            {
                int t, tt;
                scanf("%d", &t);  //有t个节点直接相连
                for (int j=0; j<t; ++j)  // 输入t个节点
                {
                    scanf("%d", &tt);
                    tt--;
                    if (j == 0)  //第一个为默认方向
                    {
                        //map[tt][i] = 0;
                        map[i][tt] = 0;
                    }
                    else
                    {
                        //map[tt][i] = 1;
                        map[i][tt] = 1;
                    }
                }
            }
            dijkstra(a);
            printf("%d ", (low[b] == maxn) ? -1 : low[b]);
        }
        return 0;
    }
    附标准代码:

    #include<iostream>
    #include<cstdio>
      #include<cstring>
      using namespace std;
      #define N 105
      #define max 0xfffffff
      int f[N],mark[N],p[N][N];
      int n,a,b;
      void Dijkstra()
     {
         int i,j,k,min;
         memset(mark,0,sizeof(mark));
         for(i=1;i<=n;i++)
             f[i]=p[a][i];
         f[a]=0;
         mark[a]=1;
         for(i=1;i<=n;i++)
         {
             min=max;
             for(j=1;j<=n;j++)
             {
                 if(!mark[j]&&f[j]<min)
                 {
                     min=f[j];
                     k=j;
                 }
             }
             if(min==max) break;
             mark[k]=1;
             for(j=1;j<=n;j++)
             {
                 if(!mark[j]&&f[k]+p[k][j]<f[j])
                     f[j]=f[k]+p[k][j];
             }
         }
         if(f[b]==max)   printf("-1 ");
         else
             printf("%d ",f[b]);
     }
     int main()
     {
         int i,j;
         while(scanf("%d%d%d",&n,&a,&b)!=EOF)
         {
             int c;
             for(i=1;i<=n;i++)
                 for(j=1;j<=n;j++)
                     p[i][j]=max;
             for(i=1;i<=n;i++)
             {
                 scanf("%d",&c);
                 if(c==0)  continue;//第一次的时候忘了考虑c的取值,所以Time Limit Exceeded了一次
                 int d;
                 scanf("%d",&d);
                 p[i][d]=0;
                 c-=1;
                 while(c--)
                 {
                     scanf("%d",&d);
                     p[i][d]=1;
                 }
             }
             Dijkstra();
         }
         return 0;
     }

  • 相关阅读:
    day_13
    day_12
    day_11
    day_10
    day_09
    day_08
    day_07
    day_06
    cmder 基本配置和使用
    php自动加载
  • 原文地址:https://www.cnblogs.com/icode-girl/p/4581461.html
Copyright © 2020-2023  润新知