hdu 1596 find the safest road
2012-07-25 15:12
183 查看
点击打开链接
dijkstra 的一点小变换
1,+------>*
2,int---->double
对于dijkstra算法注意:
1、单向还是双向图,考虑清楚
2、注意那个maxInt这个值超不超,够不够
3、注意是否两点间有多条路径
4、分清变量是double型的还是int型的
5、注意主函数中初始化map[][]中的点边不要搞错(注意所有初始化,正确命名好变量)
#include"stdio.h"
#include"string.h"
int n,s,e,t;
double map[1001][1001],f[1001];
int mark[1001];
double min(double x,double y)
{
return x<y?x:y;
}
void dijkstra()
{
int i,j,k;
double m;
for(i=1;i<=n;i++)
f[i]=map[s][i];
f[s]=0;
memset(mark,0,sizeof(mark));
f[s]=1;
for(i=1;i<=n;i++)
{
m=0;
for(j=1;j<=n;j++)
{
if(!mark[j]&&f[j]>m)
{
m=f[j];k=j;
}
}
if(m==0)break;
mark[k]=1;
for(j=1;j<=n;j++)
if(!mark[j]&&f[k]*map[k][j]-f[j]>0.000001)
f[j]=f[k]*map[k][j];
}
if(f[e])printf("%.3f\n",f[e]);
else printf("What a pity!\n");
}
int main()
{
int i,j;
double a;
while(scanf("%d",&n)!=-1)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%lf",&a);
map[i][j]=a;
}
mark[i]=0;
}
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&s,&e);
dijkstra();
}
}
return 0;
}
dijkstra 的一点小变换
1,+------>*
2,int---->double
对于dijkstra算法注意:
1、单向还是双向图,考虑清楚
2、注意那个maxInt这个值超不超,够不够
3、注意是否两点间有多条路径
4、分清变量是double型的还是int型的
5、注意主函数中初始化map[][]中的点边不要搞错(注意所有初始化,正确命名好变量)
#include"stdio.h"
#include"string.h"
int n,s,e,t;
double map[1001][1001],f[1001];
int mark[1001];
double min(double x,double y)
{
return x<y?x:y;
}
void dijkstra()
{
int i,j,k;
double m;
for(i=1;i<=n;i++)
f[i]=map[s][i];
f[s]=0;
memset(mark,0,sizeof(mark));
f[s]=1;
for(i=1;i<=n;i++)
{
m=0;
for(j=1;j<=n;j++)
{
if(!mark[j]&&f[j]>m)
{
m=f[j];k=j;
}
}
if(m==0)break;
mark[k]=1;
for(j=1;j<=n;j++)
if(!mark[j]&&f[k]*map[k][j]-f[j]>0.000001)
f[j]=f[k]*map[k][j];
}
if(f[e])printf("%.3f\n",f[e]);
else printf("What a pity!\n");
}
int main()
{
int i,j;
double a;
while(scanf("%d",&n)!=-1)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%lf",&a);
map[i][j]=a;
}
mark[i]=0;
}
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&s,&e);
dijkstra();
}
}
return 0;
}
相关文章推荐
- (阶段三 dijkstra1.4)HDU 1596 find the safest road(最短路的变形题:求乘积,求最大值)
- hdu 1596 find the safest road
- hdu 1596 find the safest road(spfa算法)
- hdu-1596 find the safest road(SPFA做法)
- HDU1596 find the safest road 解题报告--Dijkstra
- find the safest road HDU - 1596
- HDU 1596——find the safest road
- hdu 1596 find the safest road (最短路)
- hdu 1596 find the safest road 解题报告
- HDU 1596 find the safest road
- find the safest road&&http://acm.hdu.edu.cn/showproblem.php?pid=1596
- hdu 1596 floyd算法 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
- hdu 1596 find the safest road(最短路径)
- HDU 1596 find the safest road
- HDU 1596 find the safest road
- HDU 1596 find the safest road(SPFA)