CF715B. Complete The Graph
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的更多相关文章
- 【图论 思维】cf715B. Complete The Graph加强
zzq讲的杂题 题目大意 有一张$n$个点$m$条边的简单正权无向图,$S$到$T$的最短路为$L$,现在有一些边的边权未知,请输出任意一种满足题意的方案. $n,m\le 500000$ ...
- Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))
B. Complete The Graph time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- 【Codeforces】716D Complete The Graph
D. Complete The Graph time limit per test: 4 seconds memory limit per test: 256 megabytes input: sta ...
- CodeForces 715B Complete The Graph 特殊的dijkstra
Complete The Graph 题解: 比较特殊的dij的题目. dis[x][y] 代表的是用了x条特殊边, y点的距离是多少. 然后我们通过dij更新dis数组. 然后在跑的时候,把特殊边都 ...
- Codeforces 715B. Complete The Graph 最短路,Dijkstra,构造
原文链接https://www.cnblogs.com/zhouzhendong/p/CF715B.html 题解 接下来说的“边”都指代“边权未知的边”. 将所有边都设为 L+1,如果dis(S,T ...
- codeforces 715B:Complete The Graph
Description ZS the Coder has drawn an undirected graph of n vertices numbered from 0 to n - 1 and m ...
- 「CF716D」Complete The Graph「最短路」
题意 给定一个\(n\)个点\(m\)条边的无向图,有一些边权暂时为\(0\),你需要分配一个\([1, 10^{18}]\)的数.最终使得\(s\)到\(t\)最短路为\(L\),输出一个可行的分配 ...
- Codeforces Round #372 (Div. 1) B. Complete The Graph (枚举+最短路)
题目就是给你一个图,图中部分边没有赋权值,要求你把无权的边赋值,使得s->t的最短路为l. 卡了几周的题了,最后还是经群主大大指点……做出来的…… 思路就是跑最短路,然后改权值为最短路和L的差值 ...
- Codeforces715 B. Complete The Graph
传送门:>Here< 题意:给出一张带权无向图,其中有一些边权为0.要求将边权为0的边的边权重置为一个任意的正整数,使得从S到T的最短路为L.判断是否存在这种方案,如果存在输出任意一种 解 ...
随机推荐
- JavaScript传递参数方法
1.SetTimer传递参数 setTimeout(function (obj) { obj.myScroll = new IScroll('#wrapper', { click: true }); ...
- [译]asp-net-core-mvc-ajax-form-requests-using-jquery-unobtrusive
原文 全文源码 开始项目 项目使用了package.json'文件,添加需要的前端package到项目中.在这我们添加了jquery-ajax-unobstrusive`. { "versi ...
- 第31月第10天 tableview头部空白 Other Linker Flags rtmp
1.ios10 tableview头部空白 if (@available(iOS 11.0, *)) { self.tableView.contentInsetAdjustmentBehavior = ...
- MongoDB代码——Python篇
需要安装的库:pymongo 一.添加文档 from pymongo import MongoClient # 连接服务器 conn = MongoClient("localhost&quo ...
- 从输入URL到浏览器显示页面发生了什么
1.输入网址 当你开始输入网址比如www.cnblogs.com时游览器就可以在书签或者历史记录里面去搜索相关的网址推荐给你. 2.游览器查找域名的IP地址 ① 请求发起后,游览器首先会解析这个域名, ...
- 拷贝本地文件到docker容器
查找所有容器 docker ps -a 查找容器长ID docker inspect -f '{{.ID}}' python 拷贝本地文件到容器 docker cp 本地路径 容器长ID:容器路径
- 【easy】572. Subtree of Another Tree
判断一棵树中是否包含另一棵子树(包含是,两棵树重合处的根节点之下的子节点都相等) 有两种方法: 方法二:递归写法 //方法一:可以借鉴之前序列化的题目,如果序列化得到的序列一样就是相同的树 //方法二 ...
- pytorch查看CUDA支持情况,只需要三行代码,另附Cuda runtime error (48) : no kernel image is available for execution处理办法
import torch import torchvision print(torch.cuda.is_available()) 上面的命令只是检测CUDA是否安装正确并能被Pytorch检测到,并没 ...
- java--序列化和反序列化
一.序列化 java序列化的过程是把对象转换为字节序列的过程 序列化的两种用途: 1)把对象的字节序列永久保存大搜硬盘上,通常存放到一个文件中 2)在网络上传送对象的字节序列 jdk中的序列化API: ...
- vue性能
刚开始接触vue觉得vue真是好,用起来好方便呀,与以往的用jquery类库写逻辑,简直方便的要死.网上也都是对vue的好评,但是呢我现在的感觉就是vue真坑,真垃圾. 先说的我们项目遇到到问 ...