Codeforces Round #372 (Div. 1) B. Complete The Graph (枚举+最短路)
题目就是给你一个图,图中部分边没有赋权值,要求你把无权的边赋值,使得s->t的最短路为l。
卡了几周的题了,最后还是经群主大大指点……做出来的……
思路就是跑最短路,然后改权值为最短路和L的差值,直到最短路为L,或者每条无权边都赋值为止。
点是0~n-1的,因为这个错了好几次= =
dijkstra超时,spfa卡过。

看到很多题解说二分,但是实在不能理解那个思路阿…………
#include <bits/stdc++.h> using namespace std; typedef long long ll;
const int N = ;
const ll INF = 1e13+; int n, m, l, s, t;
struct edge { int from, to, next, w; } e[N*N];
int head[N], cntE;
void addedge(int u, int v, int w) {
e[cntE].from = u; e[cntE].to = v; e[cntE].next = head[u]; e[cntE].w = w; head[u] = cntE++;
e[cntE].from = v; e[cntE].to = u; e[cntE].next = head[v]; e[cntE].w = w; head[v] = cntE++;
} int cnt[N], pre[N];
ll d[N];
bool inq[N];
int spfa() {
queue<int> q;
memset(inq, , sizeof inq);
memset(cnt, , sizeof cnt);
for (int i = ; i <= n; ++i) d[i] = INF;
d[s] = ;
inq[s] = true;
q.push(s); while (q.size()) {
int u = q.front(); q.pop();
inq[u] = false;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to;
int c = e[i].w;
if (d[u] < INF && d[v] > d[u] + c) {
d[v] = d[u] + c;
pre[v] = u;
if (!inq[v]) {
q.push(v); inq[v] = true;
}
}
}
}
return d[t];
} vector<int> zeroedge;
int main()
{
//freopen("in.txt", "r", stdin);
while (~scanf("%d%d%d%d%d", &n, &m, &l, &s, &t)) {
memset(head, -, sizeof head); cntE = ; zeroedge.clear();
int u, v, c;
for (int i = ; i < m; ++i) {
scanf("%d%d%d", &u, &v, &c);
addedge(u, v, c==?:c);
if (c == ) zeroedge.push_back(cntE-);
}
ll tmp = spfa();
if (tmp > l) {
puts("NO");
continue;
}
if (tmp == l) { // may be dont have zero edge
puts("YES");
for (int i = ; i < cntE; i += ) {
printf("%d %d %d\n", e[i].from, e[i].to, e[i].w);
}
continue;
}
int fg = false;
for (int i = ; i < zeroedge.size(); ++i) {
int x = zeroedge[i];
e[x].w = e[x ^ ].w = l - tmp + ;
tmp = spfa();
if (tmp == l) {
fg = true;
puts("YES");
for (int i = ; i < cntE; i += ) {
printf("%d %d %d\n", e[i].from, e[i].to, e[i].w);
}
break;
} }
if (!fg) puts("NO");
}
return ;
}
Codeforces Round #372 (Div. 1) B. Complete The Graph (枚举+最短路)的更多相关文章
- Codeforces Round #372 (Div. 1) B. Complete The Graph
题目链接:传送门 题目大意:给你一副无向图,边有权值,初始权值>=0,若权值==0,则需要把它变为一个正整数(不超过1e18),现在问你有没有一种方法, 使图中的边权值都变为正整数的时候,从 S ...
- Codeforces Round #372 (Div. 2) A .Crazy Computer/B. Complete the Word
Codeforces Round #372 (Div. 2) 不知不觉自己怎么变的这么水了,几百年前做A.B的水平,现在依旧停留在A.B水平.甚至B题还不会做.难道是带着一种功利性的态度患得患失?总共 ...
- Codeforces Round #372 (Div. 2)
Codeforces Round #372 (Div. 2) C. Plus and Square Root 题意 一个游戏中,有一个数字\(x\),当前游戏等级为\(k\),有两种操作: '+'按钮 ...
- 构造图 Codeforces Round #236 (Div. 2) C. Searching for Graph
题目地址 /* 题意:要你构造一个有2n+p条边的图,使得,每一个含k个结点子图中,最多有2*k+p条边 水得可以啊,每个点向另外的点连通,只要不和自己连,不重边就可以,正好2*n+p就结束:) */ ...
- 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 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))
B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- B. Complete the Word(Codeforces Round #372 (Div. 2)) 尺取大法
B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #372 (Div. 2) A B C 水 暴力/模拟 构造
A. Crazy Computer time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces 715A & 716C Plus and Square Root【数学规律】 (Codeforces Round #372 (Div. 2))
C. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- Java发送Http请求
package com.liuyu.test; import java.io.BufferedReader; import java.io.IOException; import java.io.In ...
- Flex 国际化(flex Localize)
先说编译到主程序中去的方法: 1.创建资源文件夹 譬如可以在src文件夹下创建Locale文件夹,然后在此文件夹再次创建每个地区的资源文件夹,譬如de_DE,zh_CN. 然后分别创建后缀名为.pro ...
- BZOJ 2440 完全平方数(莫比乌斯-容斥原理)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2440 题意:给定K.求不是完全平方数(这里1不算完全平方数)的倍数的数字组成的数字集合S ...
- Android开发之MD5加密
将字符串进行MD5加密,返回加密后的字符串 public static String encode(String password) { try { StringBuffer sb = new Str ...
- window注册表
打开注册表: 可以用快捷键 win + r ,然后输入 Regedit 回车,会打开注册表. 注册表添加一个键值对到 操作如下: 1.先创建一个 .reg 后缀的文件. 2.文件内容如下: Wind ...
- 转:表单中Readonly和Disabled的区别(HTML中使用javascript解除禁止input输入框代)
转:表单中Readonly和Disabled的区别 参考资料: disabled和readonly区别: 参考博文1地址:http://blog.csdn.net/symgdwyh/article/d ...
- MyBatis 实践 -配置
MyBatis 实践 标签: Java与存储 Configuration mybatis-configuration.xml是MyBatis的全局配置文件(文件名任意),其配置内容和顺序如下: pro ...
- snv的绑定,检出,同步
svn安装 http://www.android100.org/html/201511/15/196792.html svn绑定Studio 显示svn图标 效果图
- CodePage代码,MultiByteToWideChar
Identifier .NET Name Additional information 37 IBM037 IBM EBCDIC US-Canada 437 IBM437 OEM United Sta ...
- 【Servlet】doGet()与doPost()的区别
doGet与doPost的区别 .Servlet接口只定义了一个服务方法--service .当发出客户端请求时,调用service方法并传递一个请求和响应对象 .使用时经常在doPost()中调用d ...