HDU 1596 find the safe road (shortest path dijkstra SPFA Floyd)

Problem Description
XX planet has many cities, each city has one or more flight paths, but not all roads are very safe, each road has a safety factor s,s is a real number between 0 and 1 (including 0, 1), a safe degree of passage P from u to v is Safe(P) = s(e1)*s(e2)... * s(ek) e1,e2,ek is the edge of P, now 8600 want to go out to travel, in the face of so many roads, he wants to find the safest way. But 8600's math is not good. I would like to ask you to help me. uuuuuuuuuuu

Input
The input includes multiple test instances, each of which includes:
The first line: n. N denotes the number of cities n<=1000;
Next, an n*n matrix represents the safety factor between the two cities. (0 can be understood as there is no direct access between the two cities.)
Then there are Q 8600 routes to travel. Each line has two numbers, indicating the city where 8600 is located and the city to go.

Output
If 86 can't reach his destination, output "What a pity!"
The other outputs the safety factor of the safest road between the two cities, retaining three decimal places.

Sample Input
3
1 0.5 0.5
0.5 1 0.4
0.5 0.4 1
3
1 2
2 3
1 3

Sample Output
0.500
0.400
0.500
Solution: Simple shortest path template problem, just change the condition of relaxation operation of the previous shortest path. It's very simple, but I've been looking for BUG for a long time because of the subscription problem. Finally, I found that the problem appeared here. I'm a bloody old man.
dijkstra:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f

using namespace std;

double Map[1005][1005];
int n,Q;
void Dijkstra(int s,int e)
{
    int vis[1005],k;
    double p[1005],Mindist;
    memset(vis,0,sizeof(vis));
    for(int i=0;i<n;i++)
        p[i] = Map[s-1][i];
    vis[s-1] = 1;
    p[s-1] = 1.0;
    for(int i = 0; i < n; i ++){
        Mindist = 0;
        for(int j = 0; j < n; j ++)
            if(vis[j] == 0 && Mindist < p[j]){
                Mindist = p[j];
                k = j;
            }
        vis[k] = 1;
        for(int j = 0; j < n; j ++)
            if(vis[j] == 0 && p[j] < Map[k][j]*Mindist)
                 p[j] = Map[k][j]*Mindist;
    }
    if(p[e-1])
        printf("%.3lf\n",p[e-1]);
    else
        printf("What a pity!\n");
}
int main()
{
    int s,e;
    while(cin>>n){
       for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                scanf("%lf",&Map[i][j]);
       cin>>Q;
       for(int i=0;i<Q;i++)
       {
           scanf("%d %d",&s,&e);
           Dijkstra(s,e);
       }
    }
    return 0;
}

SPFA

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f

using namespace std;

struct Node
{
    int v;
    double w;
};
vector<Node> Map[1005];
double dist[1005];
int vis[1005];
int n,q;
void SPFA(int s,int e)
{
    int u,v;
    queue<int> Q;
    for(int i = 0; i <= n; i++)
        dist[i] = -INF;
    memset(vis,0,sizeof(vis));
    dist[s-1] = 1.0;
    vis[s-1] = 1;
    Q.push(s-1);
    while(!Q.empty()){
        u = Q.front();Q.pop();vis[u] = 0;
        for(int i = 0; i < Map[u].size(); i ++){
            v = Map[u][i].v;
            double w = Map[u][i].w;
            if(dist[v] < dist[u] * w){
                dist[v] = dist[u] * w;
                if(vis[v] == 0){
                    vis[v] = 1;
                    Q.push(v);
                }
            }
        }
    }
    if(dist[e-1]) printf("%.3lf\n",dist[e-1]);
    else printf("What a pity!\n");
}
int main()
{
    int s,e;
    Node t;
    while(cin>>n){
       for(int i=0;i<=n;i++)
            Map[i].clear();
       for(int i=0;i<n;i++)
            for(int j=0;j<n;j++){
                scanf("%lf",&t.w);
                t.v = j;
                Map[i].push_back(t);
            }
       cin>>q;
       for(int i=0;i<q;i++)
       {
           scanf("%d %d",&s,&e);
           SPFA(s,e);
       }

    }
    return 0;
}

Floyd

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f

using namespace std;

double Map[1005][1005];
int n,Q;
void Floyd()
{
    for(int k=0;k<n;k++)
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                if(Map[i][j]<Map[i][k]*Map[k][j])
                    Map[i][j]=Map[i][k]*Map[k][j];
}
int main()
{
    int s,e;
    while(cin>>n){
       for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                scanf("%lf",&Map[i][j]);
       Floyd();
       cin>>Q;
       for(int i=0;i<Q;i++)
       {
           scanf("%d %d",&s,&e);
           if(Map[s-1][e-1])
                printf("%.3lf\n",Map[s-1][e-1]);
           else
                cout<<"What a pity!"<<endl;
       }

    }
    return 0;
}

Posted by deltatango5 on Mon, 10 Dec 2018 19:21:05 -0800