find the safest road HDU - 1596

城市与城市之间有安全指数,指数越大越安全,0即两城市不连通,安全度等于路线的安全指数相乘。输入两城市间最大的安全度。题目输入先输入连接矩阵,再输入起点终点。

链接http://acm.hdu.edu.cn/showproblem.php?pid=1596

ac:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1010;
const double eps = 1e-4;
double d[N], w[N][N];
bool vis[2000];
int t,n;
double maxn;

void dij( int s )
{
    memset(vis, 0, sizeof(vis) );
    memset(d,0,sizeof(d));
    d[s] = 1;
    for ( int i = 1; i <= n; i++ )
    {
        int u = 0;
        for ( int j = 1; j <= n; j++ )
        {
            if ( !vis[j] && d[j] > d[u] )
            {
                u = j;
            }
        }
        vis[u] = true;
        for ( int j = 1; j <= n; j++ )
        {
            if ( !vis[j] && w[u][j] > eps )
            {
                if ( d[u] * w[u][j] > d[j] )
                {
                    d[j] = d[u] * w[u][j];
                }
            }
        }
    }
}
int main()
{
    while(cin>>n)
    {
    
  
        memset(w,0,sizeof(w));
        for(int i=1; i<=n; i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%lf",&w[i][j]);
            }
        }
    
        cin>>t;
        for(int i=1;i<=t;i++)
        {
            int s,e;
            scanf("%d %d",&s,&e);
            dij(s);
            if(d[e]==0)
                printf("What a pity!\n");
            else
                printf("%.3f\n",d[e]);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/henu_xujiu/article/details/79368862