Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
乘积最短路spfa 63ms#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator