Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

乘积最短路spfa 63ms

Posted by 15211160230 at 2016-09-08 09:59:10 on Problem 2472
#include <stdio.h>
#include <memory.h>
#include <queue>
#define MAXN 101
using namespace std;
typedef struct
{	int start,end;
	int pos;
	int next;
}Edge;
Edge de[MAXN*MAXN];
int vexnum,edgenum,headlist[MAXN];
bool visited[MAXN];
double dis[MAXN];
void AddEdge(int a,int b,int c)
{	de[edgenum].start=a;
	de[edgenum].end=b;
	de[edgenum].pos=c;
	de[edgenum].next=headlist[a];
	headlist[a]=edgenum;
	edgenum++;	
}
void spfa(int k)
{	queue<int>q;
	int i,temp,x,y,max;
	double t;
	memset(visited,0,sizeof(visited));
	memset(dis,0,sizeof(dis));
	q.push(k),visited[k]=1,dis[k]=1;
	while(!q.empty())
	{	x=q.front();
		q.pop();
		visited[x]=0;
		for(i=headlist[x];i!=-1;i=de[i].next)
		{	y=de[i].end;
			if(dis[y]<(t=(double)dis[x]*de[i].pos/100))
			{	dis[y]=t;
				if(!visited[y])
					q.push(y),visited[y]=1;				
			}
		}		
	}
}
int main()
{	int n,m,i;
	int a,b,c;
	while(scanf("%d",&n)&&n!=0)
	{	scanf("%d",&m);
		vexnum=n,edgenum=0;
		memset(headlist,-1,sizeof(headlist));
		while(m--)
		{	scanf("%d %d %d",&a,&b,&c);
			AddEdge(a,b,c);
			AddEdge(b,a,c);
		}
		spfa(1);
		printf("%.6lf percent\n",100*dis[n]);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator