首页 > 其他分享 >【模板】逆单源最短(反向建图) + spfa

【模板】逆单源最短(反向建图) + spfa

时间:2023-03-30 21:33:59浏览次数:51  
标签:tmp dist idx int 建图 单源 spfa st

题目要求:不仅要求单源最短路径,还要求其余点到该点的最短路径

做法:建立反图求逆单源最短路径,至于单源最短路径选择合适于题目即可

参考题目

 1 #include <iostream>
 2 #include <queue>
 3 #include <cstring>
 4 
 5 using namespace std;
 6 
 7 typedef long long LL;
 8 typedef pair<int, int> PII;
 9 typedef pair<int, PII> PIP;
10 
11 const int N = 1000010;
12 
13 int h[N], e[N], ne[N], idx;
14 int w[N], dist[N];
15 bool st[N];
16 PIP tmp[N];
17 
18 int n, m;
19 
20 void add(int a, int b, int c)
21 {
22     e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++ ;
23 }
24 
25 void spfa()
26 {
27     memset(dist, 0x3f, sizeof dist);
28     dist[1] = 0;
29     
30     queue<int> q;
31     st[1] = true;
32     q.push(1);
33     
34     while (q.size())
35     {
36         int t = q.front();
37         q.pop();
38         st[t] = false;
39         
40         for (int i = h[t]; i != -1; i = ne[i])
41         {
42             int j = e[i];
43             
44             if (dist[j] > dist[t] + w[i])
45             {
46                 dist[j] = dist[t] + w[i];
47                 if (!st[j])
48                 {
49                     st[j] = true;
50                     q.push(j);
51                 }
52             }
53         }
54     }
55 }
56 
57 int main()
58 {
59     ios::sync_with_stdio(0);
60     cin.tie(0);
61     
62     cin >> n >> m;
63     
64     for(int i = 0; i < m; i ++ )
65     {
66         int a, b, c;
67         cin >> a >> b >> c;
68         tmp[i] = {a, {b, c}};
69     }
70     
71     memset(h, -1, sizeof h);
72     for (int i = 0; i < m; i ++ ) add(tmp[i].first, tmp[i].second.first, tmp[i].second.second);
73     
74     spfa();
75     
76     LL sum = 0;
77     for (int i = 1; i <= n; i ++ ) sum += dist[i];
78     
79     memset(h, -1, sizeof h);
80     memset(st, 0, sizeof st);
81     idx = 0;
82     
83     for (int i = 0; i < m; i ++) add(tmp[i].second.first, tmp[i].first, tmp[i].second.second);
84     
85     spfa();
86     
87     for (int i = 1; i <= n; i ++ ) sum += dist[i];
88     
89     cout << sum;
90 }
Hello World

 

   

标签:tmp,dist,idx,int,建图,单源,spfa,st
From: https://www.cnblogs.com/helloworld-congqiancongqian/p/17273338.html

相关文章

  • 5098: Sweet Butter spfa
    描述  FarmerJohnhasdiscoveredthesecrettomakingthesweetestbutterinallofWisconsin:sugar.Byplacingasugarcubeoutinthepastures,heknowstheN(1<=N<=500)cowswilllickitandthuswillproducesuper-sweetbutterwhichcan......
  • 最小费用最大流( spfa )
     constintN=5005,M=1e5+100;#defineinf1e9intall=1,hd[N],go[M],w[M],nxt[M],cost[M];intS,T,n,m;intpre[N],mn[N],dis[N],vis[N],ans=0;void......
  • SPFA
    importjava.util.Arrays;importjava.util.LinkedList;importjava.util.Queue;publicclassSPFA{ publicstaticvoidmain(String[]args){ } //边存......
  • AcWing3305 -- 建图
    1.题目描述给定我们一些初始作物,和作物之间杂交的规则(作物\(a\)和作物\(b\)杂交产生种子\(c\),花费作物\(a\)和作物\(b\)成熟时间的最大值),让我们求,某个作物\(T......
  • 常见网络流建图
    梳理一些常见的网络流建图模型。disclaimer:以下名词,模型皆本人所造,不保证合理性及正确性。选择式例题:LuoguP3254建立\(\{A_i\}\)表示选择对象,\(\{B_j\}\)表示被选......
  • 七牛云+picGo:搭建图床
    【场景】:图床就是图片的云存储。比如在用markdown记笔记时,需要插入图片,但是这个图片是本地的,如果你把md给别人或者传到网上,就无法显示图片了。【解决】:1。注册......
  • 使用PicGo和Gitee搭建图床
    使用PicGo和Gitee搭建图床在用工具写文章的过程中,如果导入的图片存在本地的话,当我们把文章上传到博客网站去就没法显示了,就算一个图一个图的复制粘贴上去,想一篇文章移植到......
  • Bellman_ford和spfa算法
    Bellman_ford算法bellman_ford算法在要求起点到终点存在负权边,要求在指定k步(这是spfa无法替代的)bellman_ford和spfa都可以判断图中有无负权环......
  • HDFS写操作(简单源码解读)
    HDFS最重要的就是写流程了,学校老师教的时候也是重点介绍这个过程(虽然我并没有在任何面试中被问到过)。下面从画图和文字两个过程介绍写流程,这次读了源代码之后对整个过程更......
  • linux(华为云)使用tomcat搭建图片服务器(保姆级别)
    参考文章:linux安装java:https://www.jianshu.com/p/3954fdf6063alinux安装tomcat:https://blog.csdn.net/m0_59347746/article/details/125716012tomcat搭建图片服务器:htt......