URAL 1934 Black Spot --- 最短的简单修改
2015-10-26 20:44
351 查看
右侧是1。维护的同时保持最短路p值至少,我有直接存款(1-p)。该概率不满足,为了使这个值极大。
#include <iostream> #include <cstdlib> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x3f3f3f3f #define eps 1e-6 #define ll long long const int maxn=100010; const int maxm=100010; using namespace std; struct node { int v,w,next; double p; }e[maxm<<1]; int vis[maxn],h,head[maxn],n,m,d[maxn],pre[maxn]; double p[maxn]; void addedge(int a,int b,double c) { e[h].v=b; e[h].w=1; e[h].p=c; e[h].next=head[a]; head[a]=h++; } void spfa(int s) { int x,v,i; for(i=0;i<=n;i++) p[i]=0,d[i]=inf; memset(vis,0,sizeof vis); memset(pre,-1,sizeof pre); p[s]=1,vis[s]=1,d[s]=0; queue<int> q; q.push(s); while(!q.empty()) { x=q.front(); q.pop(); vis[x]=0; for(i=head[x];i!=-1;i=e[i].next) { v=e[i].v; if(d[v]>d[x]+1) { d[v]=d[x]+1; p[v]=p[x]*e[i].p; pre[v]=x; if(!vis[v]) { vis[v]=1; q.push(v); } } else if(d[v]==d[x]+1) { if(p[v]<p[x]*e[i].p) { p[v]=p[x]*e[i].p; pre[v]=x; if(!vis[v]) { vis[v]=1; q.push(v); } } } } } return ; } int flag; void output(int x) { if(pre[x]!=-1) output(pre[x]); if(flag) flag=0; else putchar(' '); printf("%d",x); } int main() { int a,b,s,t; double c; while(~scanf("%d%d",&n,&m)) { h=0; memset(head,-1,sizeof head); scanf("%d%d",&s,&t); while(m--) { scanf("%d%d%lf",&a,&b,&c); addedge(a,b,1-c/100); addedge(b,a,1-c/100); } spfa(s); printf("%d %.8lf\n",d[t]+1,1-p[t]); flag=1; output(t); puts(""); } return 0; }
相关文章推荐
- 判断无向图是否是一棵树
- 皮蛋拌豆腐引发的——沟通管理的思考(丁仿)
- JS 指定天数倒计时 setInterval()
- 关于php在服务器上面数据库配置问题
- mysql安装
- 局部变量,静态局部变量,全局变量,静态全局变量在内存中的存放区别(转)
- Java虚拟机原理----JVM运行时数据区(转)
- CDN 机制
- Unable to execute dex: method ID not in [0, 0xffff]: 65536
- android orhanobut logger打印那个类第几行调用log输出,在输出日志上上点击跳转到源代码
- Remove Linked List Elements
- 杭电ACM-HDU1002-A+B ProblemII
- 【二分+最小生成树】bzoj2654 tree
- 两个栈实现队列与两个队列实现栈
- Leetcode -- Word Search
- jquery ajax提交表单数据的两种方式
- 20135321余佳源——信息安全系统设计基础第七周学习总结
- IOS 第三方库- AFNetworking
- 人数骤增,对于管理的变化,主要是两个问题
- Android如何创建背景透明的Dialog