layout: post

title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)

author: "luowentaoaa"

catalog: true

mathjax: true

tags:

- 最短路

- BellmanFord

- 图论

- 训练指南

- 差分约束


Halum

UVA - 11478

题意

带权有向图,每个点都可以有如下操作:令从ta出发的每一条边增加d,终止于ta的每一条边减小d

最后让所有边权的最小值非负且尽量大

题解

考虑每条边的约束,di表示i的halum量

w-dv+du>0

dv-du<w

但求解这个差分约束系统只是让这组不等式成立,最长路和最短路控制的都是单个d的最值而不是最小值最大

那如何最小值最大呢?

二分答案......

那么不等式变为dv-du<w-mid,成立的话说明经过操作后边权可以都比mid大

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const ll mod=998244353;
  5. const int maxn=2700+50;
  6. const ll inf=0x3f3f3f3f3f3f3f3fLL;
  7. struct Edge
  8. {
  9. int from, to;
  10. int dist;
  11. Edge() {}
  12. Edge(int u, int v, int d) : from(u), to(v), dist(d) {}
  13. };
  14. struct BellmanFord{
  15. int n,m;
  16. vector<Edge>edges;
  17. vector<int> G[maxn];
  18. bool inq[maxn]; /// 是否在队列中
  19. int d[maxn]; /// s到各个点的距离 double 要改成double类型
  20. int p[maxn]; /// 最短路中的上一条弧
  21. int cnt[maxn]; /// 进队次数
  22. void init(int n){
  23. this->n=n;
  24. for(int i=0;i<n;i++)G[i].clear();
  25. edges.clear();
  26. }
  27. void AddEdge(int from, int to, int dist)
  28. {
  29. edges.emplace_back(from, to, dist);
  30. m = edges.size();
  31. G[from].push_back(m - 1);
  32. }
  33. bool bellmanford(int s){
  34. queue<int>Q;
  35. memset(inq,0,sizeof(inq));
  36. memset(cnt,0,sizeof(cnt));
  37. for(int i = 0; i < n; i++) { d[i] = 0; inq[0] = true; Q.push(i); } ///如果只判负环用这个
  38. //for(int i=0;i<n;i++)d[i]=inf;
  39. //d[s]=0;inq[s]=true;Q.push(s);
  40. while(!Q.empty()){
  41. int u=Q.front();
  42. Q.pop();
  43. inq[u]=false;
  44. for(auto& id:G[u]){
  45. Edge& e=edges[id];
  46. if(d[u]<inf && d[e.to]>d[u]+e.dist){
  47. d[e.to]=d[u] + e.dist;
  48. p[e.to]=id;
  49. if(!inq[e.to]){
  50. Q.push(e.to);
  51. inq[e.to]=true;
  52. if(++cnt[e.to]>n)return true;
  53. }
  54. }
  55. }
  56. }
  57. return false;
  58. }
  59. };
  60. BellmanFord solver;
  61. bool test(int x){
  62. for(int i=0;i<solver.m;i++)
  63. solver.edges[i].dist-=x;
  64. bool ret=solver.bellmanford(0);
  65. for(int i=0;i<solver.m;i++)
  66. solver.edges[i].dist+=x;
  67. return !ret;
  68. }
  69. int main()
  70. {
  71. std::ios::sync_with_stdio(false);
  72. std::cin.tie(0);
  73. std::cout.tie(0);
  74. int n,m;
  75. while(cin>>n>>m){
  76. solver.init(n);
  77. int ub=0;
  78. while(m--){
  79. int u,v,d;
  80. cin>>u>>v>>d;ub=max(ub,d);
  81. solver.AddEdge(u-1,v-1,d);
  82. }
  83. if(test(ub+1))cout<<"Infinite"<<endl;
  84. else if(!test(1))cout<<"No Solution"<<endl;
  85. else{
  86. int l=2,r=ub,ans=1;
  87. while(l<=r){
  88. int mid=(l+r)/2;
  89. if(test(mid)){ans=mid;l=mid+1;}
  90. else r=mid-1;
  91. }
  92. cout<<ans<<endl;
  93. }
  94. }
  95. return 0;
  96. }

训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)的更多相关文章

  1. 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)

    layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...

  2. 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)

    layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...

  3. 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)

    layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...

  4. 训练指南 UVA - 11419(二分图最小覆盖数)

    layout: post title: 训练指南 UVA - 11419(二分图最小覆盖数) author: "luowentaoaa" catalog: true mathjax ...

  5. 训练指南 UVA - 11383(KM算法的应用 lx+ly >=w(x,y))

    layout: post title: 训练指南 UVA - 11383(KM算法的应用 lx+ly >=w(x,y)) author: "luowentaoaa" cata ...

  6. 训练指南 UVA - 11354(最小生成树 + 倍增LCA)

    layout: post title: 训练指南 UVA - 11354(最小生成树 + 倍增LCA) author: "luowentaoaa" catalog: true ma ...

  7. 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)

    layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...

  8. UVA 11478 Halum(用bellman-ford解差分约束)

    对于一个有向带权图,进行一种操作(v,d),对以点v为终点的边的权值-d,对以点v为起点的边的权值+d.现在给出一个有向带权图,为能否经过一系列的(v,d)操作使图上的每一条边的权值为正,若能,求最小 ...

  9. 算法竞赛入门经典训练指南——UVA 11300 preading the Wealth

    A Communist regime is trying to redistribute wealth in a village. They have have decided to sit ever ...

随机推荐

  1. [Leetcode] Populating next right pointer in each node ii 填充每个节点的右指针

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  2. [Leetcode] search a 2d matrix 搜索二维矩阵

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  3. android OTA升级包制作

    0.签名 java -Xmx2048m -jar out/host/linux-x86/framework/signapk.jar -w build/target/product/security/t ...

  4. bzoj 2425 [HAOI2010]计数 dp+组合计数

    [HAOI2010]计数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 451  Solved: 289[Submit][Status][Discus ...

  5. Python爬虫学习笔记之爬取新浪微博

    import requests from urllib.parse import urlencode from pyquery import PyQuery as pq from pymongo im ...

  6. 转:Mybatis系列之集合映射

    转:Mybatis系列之集合映射 上篇文章我们讲了关联映射,实现了销售与登录用户之间的关联.本文我们接着来讲一讲集合映射,实现销售与客户的多对多关系. 实现销售与客户多对多关系 本文中仍延用<M ...

  7. jsp分页完善版

    明天要考网络工程师了,而且这两天校运会,把那个分页的完善了下,明天考试,祈祷吧,我根本都没看书啊,所以只能去长见识了.100大洋啊,下个学期我想考考证了,不然以后出去麻烦了.呵呵,不多说还是说说自己对 ...

  8. [Leetcode Week6]Best Time to Buy and Sell Stock

    Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...

  9. HDU1503(LCS,记录路径)

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  10. url 拼接的一个模块furl

    from furl import furl getlongtexturl="https://weibo.com/p/aj/mblog/getlongtext" params={ & ...