| Description
In the movie "Blues Brothers", the orphanage where Elwood and Jack were raised may be sold to the Board of Education if they do not pay 5000 dollars in taxes at the Cook Country Assessor's Office in Chicago. After playing a gig in the Palace Hotel ballroom to earn these 5000 dollars, they have to find a way to Chicago. However, this is not so easy as it sounds, since they are chased by the Police, a country band and a group of Nazis. Moreover, it is 106 miles to Chicago, it is dark and they are wearing sunglasses.
As they are on a mission from God, you should help them find the safest way to Chicago. In this problem, the safest way is considered to be the route which maximises the probability that they are not caught. Input
The input contains several test cases.
Each test case starts with two integers n and m (2 <= n <= 100 , 1 <= m <= n*(n-1)/2). n is the number of intersections, m is the number of streets to be considered. The next m lines contain the description of the streets. Each street is described by a line containing 3 integers a, b and p (1 <= a, b <= n , a != b, 1 <= p <= 100): a and b are the two end points of the street and p is the probability in percent that the Blues Brothers will manage to use this street without being caught. Each street can be used in both directions. You may assume that there is at most one street between two end points. The last test case is followed by a zero. Output
For each test case, calculate the probability of the safest path from intersection 1 (the Palace Hotel) to intersection n (the Honorable Richard J. Daley Plaza in Chicago). You can assume that there is at least one path between intersection 1 and n.
Print the probability as a percentage with exactly 6 digits after the decimal point. The percentage value is considered correct if it differs by at most 10-6 from the judge output. Adhere to the format shown below and print one line for each test case. Sample Input 5 7 5 2 100 3 5 80 2 3 70 2 1 50 3 4 90 4 1 85 3 1 70 0 Sample Output 61.200000 percent Source |
哎~ 最短路看成了最小生成树,wa了5发,最后还是队友做出了的,
这里我贴出来队友的floyd 我后来写的dijkstra,还有比赛的错误的最小生成树(引以为戒)
队友的:
#include<cstdio>
#include<cstring>
const int maxn=100+10;
#define INF 10000000
double dis[maxn][maxn];
int n,m;
void floyd()
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(dis[i][j]<dis[i][k]*dis[k][j])
dis[i][j]=dis[i][k]*dis[k][j];
}
}
int main()
{
int u,v,w;
while(~scanf("%d",&n),n)
{
scanf("%d",&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(i==j) dis[i][j]=0;
else
dis[i][j]=dis[j][i]=-1;
}
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
dis[u][v]=dis[v][u]=(w*1.0)/100;
}
floyd();
printf("%.6f percent\n",dis[1][n]*100);
}
return 0;
}
dijkstra:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 105
double a[N][N];
double dis[N];
int vis[N];
int n,m;
void dijkstra()
{
int i,j,now;
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[1]=1;
for(i=1;i<=n;i++)
{
now=1;
double temp=0;
for(j=1;j<=n;j++)
if(!vis[j]&&dis[j]>temp)
{
temp=dis[j];
now=j;
}
vis[now]=1;
for(j=1;j<=n;j++)
if(!vis[j]&&a[now][j]>0)
{
dis[j]=max(dis[j],dis[now]*a[now][j]);
}
}
printf("%.6f percent\n",dis[n]*100);
}
int main()
{
int i,j;
while(~scanf("%d",&n),n)
{
scanf("%d",&m);
memset(a,0,sizeof(a));
int x,y;
double len;
while(m--)
{
scanf("%d%d%lf",&x,&y,&len);
a[x][y]=a[y][x]=len*1.0/100;
}
dijkstra();
}
return 0;
}
错误的最小生成树(至今没有想通,求大神 告诉小弟原因)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define N 100005
struct stud{
int a,b;
double s;
}f[N];
int n,m;
int father[1005];
int cmp(stud a,stud b)
{
return a.s<b.s;
}
int cha(int x)
{
if(x!=father[x])
father[x]=cha(father[x]);
return father[x];
}
void inset()
{
int i;
for(i=0;i<1000;i++)
father[i]=i;
}
void fdd()
{
int i,j;
double ans=0;
for(i=0;i<m;i++)
{
inset();
double x=1;
for(j=i;j<m;j++)
{
int aa=cha(f[j].a);
int bb=cha(f[j].b);
if(aa!=bb)
{
father[aa]=bb;
x=f[j].s*x/100;
if(cha(1)==cha(n))
{
if(ans<x)
ans=x;
break;
}
}
}
}
ans=ans*100;
printf("%.6f percent\n",ans);
}
int main()
{
int i;
while(scanf("%d",&n),n)
{
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%d%d%lf",&f[i].a,&f[i].b,&f[i].s);
}
sort(f,f+m,cmp);
fdd();
}
return 0;
}
本文介绍了一个寻找从起点到终点最安全路径的问题,通过最大概率算法实现。提供了两种算法实现方式:Floyd 和 Dijkstra,同时解释了为什么最小生成树算法在此场景下不适用。
683

被折叠的 条评论
为什么被折叠?



