find the safest road HDU - 1596
2018-02-25 14:59
330 查看
城市与城市之间有安全指数,指数越大越安全,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
, w
;
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;
}
链接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
, w
;
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;
}
相关文章推荐
- hdu 1596 find the safest road(最短路径)
- hdu 1596 find the safest road (dijkstra)
- hdu 1596 floyd算法 find the safest road
- HDU 1596 find the safest road
- HDU1596 find the safest road---(最短路径dijkstra,#变形#)
- HDU 1596 find the safest road <SPFA算法的一个变形>
- HDU 1596 find the safest road
- hdu1596 find the safest road 最短路dijkstra
- HDU 1596 find the safest road
- hdu 1596 find the safest road(待改正)且已修改
- hdu 1596 find the safest road(最短路求最大值的题目,有两种稍微不同的处理方式)
- hdu 1596 find the safest road
- hdu 1596 find the safest road(最短路)
- 1596 hdu find the safest road
- HDU 1596:find the safest road【Dijkstra & SPFA】
- HDU 1596 find the safest road
- HDU 1596 find the safest road <迪杰斯特拉算法>
- hdu 1596 find the safest road(Dijkstra)
- 两道最短路裸题(HDU 2544 最短路 / HDU 1596 find the safest road)
- HDU 1596 find the safest road (最短路径----dijkstra)