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\n", (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\n");
     else
         printf("%d\n",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;
 }

 

posted on 2015-06-16 18:01  小小八  阅读(318)  评论(0编辑  收藏  举报