CF715B. Complete The Graph

题意:

给一张 n 个点,m 条边的无向图,要求设定一些边的边权
使得所有边权都是正整数,最终 S 到 T 的最短路为 L

1 ≤ n ≤ 1000, 1 ≤ m ≤ 10000


假做法:

spfa求s到t最短路且满足可变边最少

然后把不在最短路上的可变边标为inf,最短路上的可变边修改成使最短路长为L

假的原因:

其他的赋值为inf只是保证了经过其他可变边的路径不会更短,没有保证不经过其他可变边只是少经过了几条可变边、导致比最短路长的路径不会在最短路修改后更短

存在绕过某条可变边的路径p,本来不是最短路且不经过最短路上可变边x,但你修改x之后,p会变成当前最短路,于是这个做法就挂掉了。

就是说走了非可变边,把你修改的那条可变边绕过去了

修正:

真做法1:

应当选择满足d<L的路径中经过可变边最少的一条

可以在最短路上加维,\(d(i,j)\)表示1到i经过j条可变边的最短路

复杂度\(O(mn\log{mn})\)

真做法2:

随便求一条最短路,同样其他赋值inf,然后枚举最短路上的可变边,依次修改改可变边的值,修改后再求最短路看看会不会被绕过去。最后一定会收敛出答案

无解:不经过可变边就可以<L,经过可变边也比L大

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 1e5+5, M = 1e6+5;
const ll inf = 1e16;int n, m, L, s, t;
struct edge {int u, v, ne; ll w;} e[M];
struct meow {int u, v; ll w;} a[M];
int cnt=1, h[N], mark[M];
inline void ins(int u, int v, ll w) {if(w == 0) w=1, mark[cnt+1] = mark[cnt+2] = 1;e[++cnt] = (edge) {u, v, h[u], w}; h[u] = cnt;e[++cnt] = (edge) {v, u, h[v], w}; h[v] = cnt;a[cnt>>1] = (meow) {u, v, w};
}
inline void paint(int x, ll v) {a[x].w = v;e[x<<1].w = e[x<<1|1].w = v;
}ll d[N];
int inq[N], pre[N];
int q[N], head, tail;
inline void lop(int &x) {if(x==N) x = 1;}
void spfa0(int s) {memset(d, 0x3f, sizeof(d));head = tail = 1;d[s] = 0; q[tail++] = s;  inq[s] = 1;while(head != tail) {int u = q[head++]; lop(head); inq[u] = 0;for(int i=h[u]; i; i=e[i].ne) if(!mark[i]) {int v = e[i].v;if(d[v] > d[u] + e[i].w) {d[v] = d[u] + e[i].w;if(!inq[v]) q[tail++] = v, lop(tail), inq[v] = 1;}}}
}void spfa(int s) {memset(d, 0x3f, sizeof(d));memset(inq, 0, sizeof(inq));head = tail = 1;d[s] = 0; q[tail++] = s;  inq[s] = 1;while(head != tail) {int u = q[head++]; lop(head); inq[u] = 0;for(int i=h[u]; i; i=e[i].ne) {int v = e[i].v;if(d[v] > d[u] + e[i].w ) {d[v] = d[u] + e[i].w;pre[v] = i;if(!inq[v]) q[tail++] = v, lop(tail), inq[v] = 1;}}}
}
int chose[N];
vector<int> li;
int main() {//freopen("in", "r", stdin);ios::sync_with_stdio(false); cin.tie(); cout.tie();cin >> n >> m >> L >> s >> t;s++; t++;for(int i=1; i<=m; i++) {int u, v, w;cin >> u >> v >> w;u++; v++;ins(u, v, w);}spfa0(s);if(d[t] < L) {cout << "NO"; return 0;}spfa(s);if(d[t] > L) {cout << "NO"; return 0;}if(d[t] == L) {cout << "YES" << endl;for(int i=1; i <= cnt>>1; i++) cout << a[i].u-1 << ' ' << a[i].v-1 << ' ' << a[i].w << '\n';return 0;}int x = t;while(x != s) {if(mark[pre[x]]) chose[pre[x]>>1] = 1, li.push_back(pre[x]>>1);x = e[pre[x]].u;}for(int i=1; i<=cnt>>1; i++) if(mark[i<<1] && !chose[i]) paint(i, inf);for(int i=0; i<li.size(); i++) {int now = li[i];int delta = L - d[t] + 1;paint(now, delta);spfa(s);if(d[t] == L) break;}cout << "YES" << endl;for(int i=1; i <= cnt>>1; i++) cout << a[i].u-1 << ' ' << a[i].v-1 << ' ' << a[i].w << '\n';
}

ps:假做法的代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 1e5+5, M = 1e6+5;
const ll inf = 1e16;int n, m, L, s, t;
struct edge {int u, v, ne; ll w;} e[M];
struct meow {int u, v; ll w;} a[M];
int cnt=1, h[N], mark[M];
inline void ins(int u, int v, ll w) {if(w == 0) w=1, mark[cnt+1] = mark[cnt+2] = 1;e[++cnt] = (edge) {u, v, h[u], w}; h[u] = cnt;e[++cnt] = (edge) {v, u, h[v], w}; h[v] = cnt;a[cnt>>1] = (meow) {u, v, w};
}ll d[N];
int inq[N], cou[N], pre[N];
int q[N], head, tail;
inline void lop(int &x) {if(x==N) x = 1;}
void spfa0(int s) {memset(d, 0x3f, sizeof(d));head = tail = 1;d[s] = 0; q[tail++] = s;  inq[s] = 1;while(head != tail) {int u = q[head++]; lop(head); inq[u] = 0;for(int i=h[u]; i; i=e[i].ne) if(!mark[i]) {int v = e[i].v;if(d[v] > d[u] + e[i].w) {d[v] = d[u] + e[i].w;if(!inq[v]) q[tail++] = v, lop(tail), inq[v] = 1;}}}
}
namespace test {int pre[N];
void spfa1(int s) {memset(d, 0x3f, sizeof(d));head = tail = 1;d[s] = 0; q[tail++] = s;  inq[s] = 1;while(head != tail) {int u = q[head++]; lop(head); inq[u] = 0;for(int i=h[u]; i; i=e[i].ne) {int v = e[i].v;if(d[v] > d[u] + e[i].w) {d[v] = d[u] + e[i].w;pre[v] = i;if(!inq[v]) q[tail++] = v, lop(tail), inq[v] = 1;}}}
}
}
void spfa(int s) {memset(d, 0x3f, sizeof(d));memset(cou, 0x3f, sizeof(cou));memset(inq, 0, sizeof(inq));head = tail = 1;d[s] = 0; cou[s] = 0; q[tail++] = s;  inq[s] = 1;while(head != tail) {int u = q[head++]; lop(head); inq[u] = 0;for(int i=h[u]; i; i=e[i].ne) {int v = e[i].v;if(d[v] > d[u] + e[i].w || (d[v] == d[u]+e[i].w && cou[v] > cou[u] + mark[i])) {d[v] = d[u] + e[i].w;cou[v] = cou[u] + mark[i];pre[v] = i;if(!inq[v]) q[tail++] = v, lop(tail), inq[v] = 1;}}}
}
int chose[N];
int main() {//freopen("in", "r", stdin);ios::sync_with_stdio(false); cin.tie(); cout.tie();cin >> n >> m >> L >> s >> t;s++; t++;for(int i=1; i<=m; i++) {int u, v, w;cin >> u >> v >> w;u++; v++;ins(u, v, w);}spfa0(s);if(d[t] < L) {cout << "NO"; return 0;}spfa(s);if(d[t] > L) {cout << "NO"; return 0;}if(d[t] == L) {cout << "YES" << endl;for(int i=1; i <= cnt>>1; i++) cout << a[i].u-1 << ' ' << a[i].v-1 << ' ' << a[i].w << '\n';return 0;}int x = t;int flag = 0;while(x != s) {if(mark[pre[x]]) chose[pre[x]>>1] = 1, flag = pre[x] >> 1;x = e[pre[x]].u;}if(!flag) {cout << "NO"; return 0;}for(int i=1; i <= cnt>>1; i++) if(mark[i<<1] && !chose[i]) a[i].w = inf;for(int i=1; i<=cnt; i++) if(mark[i] && !chose[i>>1]) e[i].w = inf;int delta = L - d[t] + 1;a[flag].w = delta; e[flag<<1].w = e[flag<<1|1].w = delta;test::spfa1(s);if(d[t] != L) {cout << d[t] << "nooooo\n";int x = t;while(x != s) {if(test::pre[x] != pre[x]) {cout << "wrong\n";cout << mark[pre[x]] << "   " << mark[test::pre[x]] << '\n';}x = e[pre[x]].u;}}cout << "YES" << endl;for(int i=1; i <= cnt>>1; i++) cout << a[i].u-1 << ' ' << a[i].v-1 << ' ' << a[i].w << '\n';
}

CF715B. Complete The Graph相关推荐

  1. CF715B complete the gragh

    CF715B complete the gragh:无 向图,有的边权由你自己设(>0),给出一个方案让S到T最短路为L n<=1000 m<=10000 方法:1.先找一条路径使它 ...

  2. Codeforces Round #372 (Div. 1) B. Complete The Graph

    题目链接:传送门 题目大意:给你一副无向图,边有权值,初始权值>=0,若权值==0,则需要把它变为一个正整数(不超过1e18),现在问你有没有一种方法, 使图中的边权值都变为正整数的时候,从 S ...

  3. Codeforces 716D - Complete The Graph(最短路)

    题意:给定n个点,m条边,以及起点s,终点t,问你图中是否存在s->t的最短路为L,其中权值为0的可以任意修改. 思路:对给定的边分为2类,权重不为0的直接扔进去建图,权重为0的边先存起来.接着 ...

  4. Paper:《Graph Neural Networks: A Review of Methods and Applications》翻译与解读

    Paper:<Graph Neural Networks: A Review of Methods and Applications>翻译与解读 目录 <Graph Neural N ...

  5. A quick complete tutorial to save and restore Tensorflow models

    In this Tensorflow tutorial, I shall explain: How does a Tensorflow model look like? How to save a T ...

  6. Learning Human-Object Interactions by Graph Parsing Neural Networks阅读笔记

    前言 这是一篇2018年发表在ECCV上得paper,论文原文点这里. 笔记 说实话刚看到这篇文章还是有点唬人的,文中作者提出了一个网络模型叫GPNN,图解析神经网络,我以为是跟GNN有关系,花了一些 ...

  7. 2020中国大学生程序设计竞赛(CCPC)- 网络选拔赛 1002 Graph Theory Class

    题目链接 Problem Description This class is on graph theory. Mr. Kruskal teaches babies the concept of mi ...

  8. C#,图论与图算法,二分图(Bipartite Graph)的霍普克罗夫特-卡普(Hopcroft Karp)最大匹配算法与源程序

    二分图Bipartite graph 有没有可能通过数学过程找到你的灵魂伴侣?大概让我们一起探索吧! 假设有两组人注册了约会服务.在他们注册后,会向他们展示另一组人的图像并给出他们的描述.他们被要求选 ...

  9. Noip前的大抱佛脚----赛前任务

    赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...

最新文章

  1. 献给老师,我的编程之路
  2. Python 包含\u字符串转中文(\u00)
  3. ElasticSearch简单搭建
  4. 数据科学入门与实战:Matplotlib绘图Series
  5. Request库入门
  6. sift算法的主要步骤
  7. jQuery用户从服务器端注册登录
  8. ubuntu-多网卡聚合-bond技术教程
  9. office2007安装时,提示找不到Office.zh-cn下的OfficeMUI.msi解决方法
  10. Java、JSP医院药库管理系统
  11. ensp服务器配置文件,ensp中服务器的基本配置
  12. access百科 pc_PC Access SMART
  13. NetworkManager和network
  14. 项目实训--Unity多人游戏开发(八、3D音效融合AudioMixer、统一的音频播放系统)
  15. 关于STM32F103x系列ISP烧写出现“程序文件不是0x8000000和0x20000000区域的”解决办法。
  16. C++核心准则C.164:避免隐式转换运算符
  17. 【c语言进阶】大家是否对数据的存储不甚了解?本篇将揭开数据存储的神秘面纱*^____^*数据的存储(一)知识点讲解
  18. 实验三 使用CSS3
  19. SpringBoot使用druid的密码加密
  20. autoconf使用

热门文章

  1. java如何使实验箱蜂鸣器响_按键按一次蜂鸣器响一次的单片机程序
  2. 人群密度估计--Fully Convolutional Crowd Counting On Highly Congested Scenes
  3. matlab bp结果,Matlab如何处理BP网络每次运行结果不一样这个问题
  4. mysql分页案例_php+mysql 进行分页案例
  5. Linux中的输入输出管理
  6. 贪吃蛇程序 php,php,函数 Web程序 - 贪吃蛇学院-专业IT技术平台
  7. servlet的 session什么时候用_抖音什么时候用dou+
  8. p20华为云电脑白屏_永别了电脑,华为大举动:华为云电脑,重新定义个人电脑...
  9. python网络信息提取_python网络爬虫与信息提取I
  10. linux线程池实现多线程并发,基于Linux的多线程池并发Web服务器设计-电子设计工程.PDF...