HDOJ 1596 find the safest road (d||s)

题目链接:.........


直接模版

code:

#include <stdio.h>
int n = 0, used[1002];
double map[1002][1002], dis[1002];
void dijkstar(int u, int v)
{
    int i = 0, j = 0, k = 0;
    double min = 0;
    for(i = 1; i<=n; i++)
    {
        dis[i] = map[u][i];
        used[i] = 0;
    }
    used[u] = 1;
    for(i = 1; i<n; i++)
    {
        min = 0;
        for(j = 1; j<=n; j++)
            if(!used[j] && min<dis[j])
            {
                min = dis[j];
                k = j;
            }
        used[k] = 1;
        for(j = 1; j<=n; j++)
        {
            if(!used[j])
            {
                if(dis[k]*map[k][j]>dis[j])
                    dis[j] = dis[k]*map[k][j];
            }
        }
    }
}
int main()
{
    int i = 0, j = 0, m = 0, u = 0, v = 0;
    while(scanf("%d", &n) != EOF)
    {
        for(i = 1; i<=n; i++)
            for(j = 1; j<=n; j++)
                scanf("%lf",&map[i][j]);
        scanf("%d",&m);
        for(i = 0; i<m; i++)
        {
            scanf("%d %d",&u, &v);
            dijkstar(u, v);
            if(dis[v] == 0)
                printf("What a pity!\n");
            else
                printf("%.3lf\n", dis[v]);
        }
    }
    return 0;
}



spfa

code:

#include <stdio.h>
#include <string.h>
struct
{
    int v, next;
    double safe;
}edge[1002*1000];
int n = 0, head[1002], used[1002];
double dis[1002];
void spfa(int u, int v)
{
    int i = 0, p = 0, q = 0, front = 0, rear = 0, que[10000];
    for(i = 1; i<=n; i++)
    {
        used[i] = 0;
        dis[i] = 0;
    }
    dis[u] = 1;
    que[rear++] = u;
    while(front != rear)
    {
        p = que[front];
        used[p] = 0;
        front = (front+1)%10000;
        for(q = head[p]; q != -1; q = edge[q].next)
        {
            if(dis[edge[q].v]<dis[p]*edge[q].safe)
            {
                dis[edge[q].v] = dis[p]*edge[q].safe;
                if(!used[edge[q].v])
                {
                    used[edge[q].v] = 1;
                    que[rear] = edge[q].v;
                    rear = (rear+1)%10000;
                }
            }
        }
    }
}
int main()
{
    int i = 0, j = 0, m = 0, u = 0, v = 0, count = 0;
    double safe = 0;
    while(scanf("%d", &n) != EOF)
    {
        count = 0;
        memset(head, -1, sizeof(head));
        for(i = 1; i<=n; i++)
            for(j = 1; j<=n; j++)
            {
                scanf("%lf",&safe);
                edge[count].v = j;
                edge[count].safe = safe;
                edge[count].next = head[i];
                head[i] = count++;
            }
        scanf("%d",&m);
        for(i = 0; i<m; i++)
        {
            scanf("%d %d",&u, &v);
            spfa(u, v);
            if(dis[v] == 0)
                printf("What a pity!\n");
            else
                printf("%.3lf\n", dis[v]);
        }
    }
    return 0;
}


spfa

code:

#include <stdio.h>
int n = 0, used[1002];
double map[1002][1002], dis[1002];
void spfa(int u, int v)
{
    int i = 0, j = 0, front = 0, rear = 0, que[10000];
    for(i = 1; i<=n; i++)
    {
        used[i] = 0;
        dis[i] = 0;
    }
    dis[u] = 1;
    que[rear++] = u;
    while(front != rear)
    {
        i = que[front];
        used[i] = 0;
        front = (front+1)%10000;
        for(j = 1; j<=n; j++)
        {
            if(dis[j]<dis[i]*map[i][j])
            {
                dis[j] = dis[i]*map[i][j];
                if(!used[j])
                {
                    used[j] = 1;
                    que[rear] = j;
                    rear = (rear+1)%10000;
                }
            }
        }
    }
}
int main()
{
    int i = 0, j = 0, m = 0, u = 0, v = 0, count = 0;
    while(scanf("%d", &n) != EOF)
    {
        count = 0;
        for(i = 1; i<=n; i++)
            for(j = 1; j<=n; j++)
            {
                scanf("%lf", &map[i][j]);
            }
        scanf("%d",&m);
        for(i = 0; i<m; i++)
        {
            scanf("%d %d",&u, &v);
            spfa(u, v);
            if(dis[v] == 0)
                printf("What a pity!\n");
            else
                printf("%.3lf\n", dis[v]);
        }
    }
    return 0;
}




你可能感兴趣的:(IM)