题目就是给你一个图,图中部分边没有赋权值,要求你把无权的边赋值,使得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. MapReduce编程系列 — 5:单表关联

    1.项目名称: 2.项目数据: chile    parentTom    LucyTom    JackJone    LucyJone    JackLucy    MaryLucy    Ben ...

  2. RecyclerView(5)官方教程带简单示例

    Create Lists The RecyclerView widget is a more advanced and flexible version of ListView. This widge ...

  3. java 菱形

    //画菱形 一半 for(int hs=1;hs<11;hs++) //行数 { //画空格 for(int kg = 9; kg >= hs; kg--) //空格数 { System. ...

  4. mkdir -p

    git bash 或 mac terminal 我们可以使用 mkdir 命令来创建文件夹. 当前目录创建多个文件夹: $ mkdir a b c 会创建 a .b.c 三个文件夹 但是有时候我们需要 ...

  5. 字符串模式匹配sunday算法

    文字部分转自:http://www.cnblogs.com/mr-ghostaqi/p/4285868.html 代码是我自己写的 今天在做LeetCode的时候,碰到一个写字符串匹配的题目: htt ...

  6. 如何使用 Java 测试 IBM Systems Director 的 REST API

    转自: http://www.ibm.com/developerworks/cn/aix/library/au-aix-systemsdirector/section2.html 如何使用 Java ...

  7. ha_innobase::open

    http://mysql.taobao.org/monthly/2015/08/07/ /******************************************************* ...

  8. Web Api 返回参数,实现统一标准化!

    string camelCaseObj = JsonConvert.SerializeObject(data, Newtonsoft.Json.Formatting.None, new JsonSer ...

  9. Oracle 课程八之性能优化之10053事件

    一. 10053事件 当一个SQL出现性能问题的时候,可以使用SQL_TRACE 或者 10046事件来跟踪SQL. 通过生成的trace来了解SQL的执行过程. 我们在查看一条SQL的执行计划的时候 ...

  10. MySQL 5.6 复制:GTID 的优点和限制(第一部分)

    全局事务标示符(Global Transactions Identifier)是MySQL 5.6复制的一个新特性.它为维护特定的复制拓扑结构下服务器的DBA们大幅度改善他们的工作状况提供了多种可能性 ...