题目就是给你一个图,图中部分边没有赋权值,要求你把无权的边赋值,使得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 (枚举+最短路)的更多相关文章

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

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

  2. Codeforces Round #372 (Div. 2) A .Crazy Computer/B. Complete the Word

    Codeforces Round #372 (Div. 2) 不知不觉自己怎么变的这么水了,几百年前做A.B的水平,现在依旧停留在A.B水平.甚至B题还不会做.难道是带着一种功利性的态度患得患失?总共 ...

  3. Codeforces Round #372 (Div. 2)

    Codeforces Round #372 (Div. 2) C. Plus and Square Root 题意 一个游戏中,有一个数字\(x\),当前游戏等级为\(k\),有两种操作: '+'按钮 ...

  4. 构造图 Codeforces Round #236 (Div. 2) C. Searching for Graph

    题目地址 /* 题意:要你构造一个有2n+p条边的图,使得,每一个含k个结点子图中,最多有2*k+p条边 水得可以啊,每个点向另外的点连通,只要不和自己连,不重边就可以,正好2*n+p就结束:) */ ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 【Spring】如何在单个Boot应用中配置多数据库?

    原创 BOOT 为什么需要多数据库? 默认情况下,Spring Boot使用的是单数据库配置(通过spring.datasource.*配置具体数据库连接信息).对于绝大多数Spring Boot应用 ...

  2. URAL 1957 Wrong Answer 暴力

    Wrong Answer 思路: 1.先枚举4的全排列,即球赛的所有可能结果,一共4!=24种情况 2.对于每种情况,DFS 未确定的比赛 的结果,判断这种情况是否可达. 剪枝: 1.对于每种全排列, ...

  3. Javascript如何判断一个变量是数字类型?

    isNaN()不能判断一个变量是否为数字类型,isNaN(123)值为false,isNaN('123')值也为false.isNaN() 的实际作用跟它的名字isNaN并不一致,isNaN(NaN) ...

  4. 在Linux中创建静态库和动态库

    我们通常把一些公用函数制作成函数库,供其它程序使用. 函数库分为静态库和动态库两种. 静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库. 动态库在程序编译时并不会被连接到目标代码中 ...

  5. c扩展调用php的函数(调用实现php函数的c函数)

    上一次是写的c扩展调用c的标准函数,但是只能调用头文件中申明的函数,今天来说下c扩展调用实现php函数的c函数,比方说,c扩展要用到php中ip2long这个函数,但是c不可能去php中调用,肯定是去 ...

  6. trackr: An AngularJS app with a Java 8 backend – Part II

    该系列文章来自techdev The Frontend 在本系列的第一部分我们已经描述RESTful端建立在Java 8和Spring.这一部分将介绍我们的第一个用 AngularJS建造的客户端应用 ...

  7. UVa 557 (概率 递推) Burger

    题意: 有两种汉堡给2n个孩子吃,每个孩子在吃之前要抛硬币决定吃哪一种汉堡.如果只剩一种汉堡,就不用抛硬币了. 求最后两个孩子吃到同一种汉堡的概率. 分析: 可以从反面思考,求最后两个孩子吃到不同汉堡 ...

  8. poj 1330 Nearest Common Ancestors(LCA:最近公共祖先)

    多校第七场考了一道lca,那么就挑一道水题学习一下吧= = 最简单暴力的方法:建好树后,输入询问的点u,v,先把u全部的祖先标记掉,然后沿着v->rt(根)的顺序检查,第一个被u标记的点即为u, ...

  9. 分享一段H264视频和AAC音频的RTP封包代码

    1. H264视频的RTP封包 static int h264_parse(Track *tr, uint8_t *data, size_t len) { h264_priv *priv = tr-& ...

  10. java中时间格式yyyyMMddHHmmss的大小写问题

    字母     日期或时间元素 表示 示例 G Era 标志符 Text AD y 年 Year 1996 ; 96 M 年中的月份 Month July ; Jul ; 07 w 年中的周数 Numb ...