ZOJ 2797 106 miles to Chicago

Source: Internet
Author: User

The main idea, give the N nodes, M-edge, each edge of the weight of the line from the edge of the police are not caught by the probability, asked from Node A to Node B is not caught the maximum probability.

Floyd deformation, simply change the equation to max[I [j] =max (max[i] [j], max[I [k]*max[K] [j]), the initial time max[I [j] = 0, there is also a point worth noting is the weight of the edge of the topic (i.e. Probability) is between 1 and 100, and the direct multiplication can be hyperspace, so the storage is divided by 100.

#include <stdio.h>
double max[110][110];
int n,m;
void Floyd ()
{
	int i,j,k;
	for (k=1;k<=n;k++) for (
		i=1;i<=n;i++) for (
			j=1;j<=n;j++)
				if (Max[i][j]<max[i][k]*max[k][j] )
					max[i][j]=max[i][k]*max[k][j];
}
int main ()
{
	int i,j,a,b,k;
	Double p;
	while (1)
	{
		scanf ("%d%d", &n,&m);
		if (n==0) break
			;	
		for (i=1;i<=n;i++) for
			(j=1;j<=n;j++)
				max[i][j]=0;
		for (k=1;k<=m;k++)
		{
			scanf ("%d%d%lf", &a,&b,&p);
			max[a][b]=max[b][a]=p/100;
		}
		Floyd ();
		max[1][n]*=100;
		printf ("%.6lf percent\n", Max[1][n]);
	}
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.