标签:
Description
Input
Output
Sample Input
input | output |
---|---|
4 4 1 3 1 2 50 2 3 50 1 4 10 4 3 10 |
3 0.19 1 4 3 |
#include<stdio.h> #include<iostream> #include<queue> #include<string.h> #include<algorithm> using namespace std; const int maxn=110000; int n,m; bool vis[maxn]; int before[maxn],dis[maxn]; const int inf=99999999; int first[maxn]; int cnt; double tp[maxn]; struct node { int v; double p; int next; } que[maxn<<1]; void addedge(int a,int b,double c) { que[cnt].v=b; que[cnt].p=c; que[cnt].next=first[a]; first[a]=cnt; cnt++; } void spfa(int u) { memset(tp,0,sizeof(tp)); tp[u]=1; memset(vis,false,sizeof(vis)); vis[u]=true; memset(before,-1,sizeof(before)); for(int i=0;i<=n;i++) dis[i]=inf; dis[u]=0; queue<int>q; q.push(u); while(!q.empty()) { int x=q.front(); q.pop(); vis[x]=false; for(int i=first[x]; i!=-1; i=que[i].next) { int v=que[i].v; if(dis[v]>dis[x]+1) { dis[v]=dis[x]+1; before[v]=x; tp[v]=tp[x]*que[i].p; if(!vis[v]) { vis[v]=true; q.push(v); } } else if(dis[v]==dis[x]+1) { if(tp[x]*que[i].p>tp[v]) { tp[v]=tp[x]*que[i].p; before[v]=x; if(!vis[v]) { vis[v]=true; q.push(v); } } } } } return ; } int ans[maxn]; void outp(int tend){ memset(ans,0,sizeof(ans)); int cnt=-1; for(int i=tend;i!=-1;i=before[i]){ ans[++cnt]=i; } for(int i=cnt;i>=0;i--){ printf("%d%c",ans[i],i==0?‘\n‘:‘ ‘); } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { memset(first,-1,sizeof(first)); int start,tend; scanf("%d%d",&start,&tend); cnt=0; for(int i=1; i<=m; i++) { int t1,t2; double t3; scanf("%d%d%lf",&t1,&t2,&t3); addedge(t1,t2,1-t3/100); addedge(t2,t1,1-t3/100); } spfa(start); printf("%d %.8lf\n",dis[tend]+1,1-tp[tend]); outp(tend); } return 0; }
标签:
原文地址:http://www.cnblogs.com/13224ACMer/p/4685266.html