• 1934. Black Spot(spfa)


    1934

    水题 RE了N久  后来发现是无向图

      1 #include <iostream>
      2 #include<cstring>
      3 #include<algorithm>
      4 #include<stdlib.h>
      5 #include<vector>
      6 #include<cstdio>
      7 #include<queue>
      8 using namespace std;
      9 #define N 2000010
     10 #define INF 0xfffffff
     11 struct node
     12 {
     13     int u,v,next;
     14     double w;
     15 }ed[N<<1];
     16 int t,head[N],vis[N],dis[N],pa[N],o[N];
     17 double p[N];
     18 int n,m;
     19 void init()
     20 {
     21     t = 0;
     22     memset(head,-1,sizeof(head));
     23 }
     24 void add(int u,int v,int w)
     25 {
     26     ed[t].u = u;
     27     ed[t].v = v;
     28     ed[t].w = w;
     29     ed[t].next = head[u];
     30     head[u] = t++;
     31 }
     32 void spfa(int s,int e)
     33 {
     34     memset(vis,0,sizeof(vis));
     35     int i;
     36     for(i = 1; i <= n ; i++)
     37     {
     38         dis[i] = INF;
     39         p[i] = 0;
     40     }
     41     queue<int>q;
     42     dis[s] = 1;
     43     p[s] = 1;
     44     q.push(s);
     45     while(!q.empty())
     46     {
     47         int u = q.front();
     48         q.pop();
     49         vis[u] = 0;
     50         for(i = head[u] ; i != -1 ; i = ed[i].next)
     51         {
     52             int v = ed[i].v;
     53             double w = ed[i].w;
     54             if(dis[v]>=dis[u]+1)
     55             {
     56                 if(dis[v]==dis[u]+1)
     57                 {
     58                     if(p[v]<p[u]*w/100.0)
     59                     {
     60                         p[v] = p[u]*w/100.0;
     61                         pa[v] = u;
     62                     }
     63                 }
     64                 else
     65                 {
     66                     dis[v] = dis[u]+1;
     67                     p[v] = p[u]*w/100.0;
     68                     pa[v] = u;
     69                 }
     70                 if(!vis[v])
     71                 {
     72                     vis[v] = 1;
     73                     q.push(v);
     74                 }
     75             }
     76         }
     77     }
     78     printf("%d %lf
    ",dis[e],1-p[e]);
     79     int x = pa[e],g=1;
     80     o[g] = e;
     81     while(x!=s)
     82     {
     83         g++;
     84         o[g] = x;
     85         x = pa[x];
     86     }
     87     g++;
     88     o[g] = s;
     89     for(i = g ; i > 1; i--)
     90     printf("%d ",o[i]);
     91     printf("%d
    ",o[1]);
     92 }
     93 int main()
     94 {
     95     int i;init();
     96     int a,b;
     97     scanf("%d%d",&n,&m);
     98     scanf("%d%d",&a,&b);
     99     for(i = 1; i <= m ; i++)
    100     {
    101         int u,v;
    102         double w;
    103         scanf("%d%d%lf",&u,&v,&w);
    104         add(u,v,100-w);
    105         add(v,u,100-w);
    106     }
    107     spfa(a,b);
    108     return 0;
    109 }
    View Code
  • 相关阅读:
    【Web技术】561- 网站性能优化之度量篇
    【拓展】什么是Deno?跟Node.js有何区别?
    【Web技术】560- 一个简洁、有趣的无限下拉方案
    【Web技术】559- 一文带你彻底搞懂跨域
    【TS】558- 5000 多字,让你一文掌握 TS 枚举
    【拓展】你真的会写 Markdown 么?
    【调试】557- 11 个Chrome骚技巧让你为所欲为
    ios 地图
    ios 7 20像素解决
    保存图片到相册
  • 原文地址:https://www.cnblogs.com/shangyu/p/3364357.html
Copyright © 2020-2023  润新知