HDU 1596 find the safest road(Dijkstra,数据格式没定义好wrong了无数发)

题目地址:点击打开链接

思路:应该定义为double却定义为int,wrong了无数发,注意起点和终点一样的情况

AC代码1:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

using namespace std;

const int maxn = 1010;
double map1[maxn][maxn];
double lowdist[maxn];
int visit[maxn];
int n;

void Dijkstra(int st,int ed)
{
    int i,j,k;
    memset(visit,0,sizeof(visit));
    for(i=1; i<=n; i++)
    {
        lowdist[i] = map1[st][i];
    }
    lowdist[st] = 1;//起点和终点一样的情况
    visit[st] = 1;
    for(i=1; i<n; i++)
    {
        double max2 = -1;//注意用double,用int wrong了无数发
        for(j=1; j<=n; j++)
        {
            if(!visit[j] && lowdist[j] > max2)
            {
                k = j;
                max2 = lowdist[j];
            }
        }
        visit[k] = 1;
        for(j=1; j<=n; j++)
        {
            if(!visit[j] && lowdist[k] * map1[k][j] > lowdist[j])
            {
                lowdist[j] = lowdist[k] * map1[k][j];
            }
        }
    }
}

int main()
{
    int i,j,q;
    while(scanf("%d",&n) != EOF)
    {
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=n; j++)
            {
                scanf("%lf",&map1[i][j]);
            }
        }
        scanf("%d",&q);
        int st,ed;
        for(i=0; i<q; i++)
        {
            scanf("%d%d",&st,&ed);
            Dijkstra(st,ed);
            if(lowdist[ed] != 0)
            {
                printf("%.3lf\n",lowdist[ed]);
            }
            else
            {
                printf("What a pity!\n");
            }
        }
    }
    return 0;
}
AC代码2:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

using namespace std;

const int maxn = 1010;
double map1[maxn][maxn];
double lowdist[maxn];
int visit[maxn];
int n;

void Dijkstra(int st,int ed)
{
    int i,j,k;
    memset(visit,0,sizeof(visit));
    for(i=1; i<=n; i++)
    {
        lowdist[i] = map1[st][i];
    }
    lowdist[st] = 1;
    visit[st] = 1;
    for(i=1; i<n; i++)
    {
        double max2 = 0;
        k = 0;
        for(j=1; j<=n; j++)
        {
            if(!visit[j] && lowdist[j] > max2)
            {
                k = j;
                max2 = lowdist[j];
            }
        }
        if(k == 0)//当k为0,表示剩下的路都不能走了,退出来
            return;
        visit[k] = 1;
        for(j=1; j<=n; j++)
        {
            if(!visit[j] && lowdist[k] * map1[k][j] > lowdist[j])
            {
                lowdist[j] = lowdist[k] * map1[k][j];
            }
        }
    }
}

int main()
{
    int i,j,q;
    while(scanf("%d",&n) != EOF)
    {
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=n; j++)
            {
                scanf("%lf",&map1[i][j]);
            }
        }
        scanf("%d",&q);
        int st,ed;
        for(i=0; i<q; i++)
        {
            scanf("%d%d",&st,&ed);
            Dijkstra(st,ed);
            if(lowdist[ed] != 0)
            {
                printf("%.3lf\n",lowdist[ed]);
            }
            else
            {
                printf("What a pity!\n");
            }
        }
    }
    return 0;
}



你可能感兴趣的:(HDU 1596 find the safest road(Dijkstra,数据格式没定义好wrong了无数发))