CF1430G.Yet Another DAG Problem

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a directed acyclic graph (a directed graph that does not contain cycles) of nn vertices and mm arcs. The ii-th arc leads from the vertex xix_i to the vertex yiy_i and has the weight wiw_i.

Your task is to select an integer ava_v for each vertex vv, and then write a number bib_i on each arcs ii such that bi=axi−ayib_i = a_{x_i} - a_{y_i}. You must select the numbers so that:

  • all bib_i are positive;
  • the value of the expression ∑i=1mwibi\sum \limits_{i = 1}^{m} w_i b_i is the lowest possible.

It can be shown that for any directed acyclic graph with non-negative wiw_i, such a way to choose numbers exists.

给你一个包含 nn 个顶点和 mm 条有向边的有向无环图(即不含环的有向图)。第 ii 条边从顶点 xix_i 指向顶点 yiy_i,其权值为 wiw_i。

你的任务是为每个顶点 vv 选定一个整数 ava_v,并在每条边 ii 上写一个数 bib_i,使得 bi=axi−ayib_i = a_{x_i} - a_{y_i}。你需要选择这些数,满足以下条件:

  • 所有 bib_i 均为正数;
  • 表达式 ∑i=1mwibi\sum \limits_{i = 1}^{m} w_i b_i 的值尽可能小。

可以证明:对于任意边权 wiw_i 非负的有向无环图,总存在满足上述要求的选取方式。

输入格式

The first line contains two integers nn and mm (2≤n≤182 \le n \le 18; 0≤m≤n(n−1)20 \le m \le \dfrac{n(n - 1)}{2}).

Then mm lines follow, the ii-th of them contains three integers xix_i, yiy_i and wiw_i (1≤xi,yi≤n1 \le x_i, y_i \le n, 1≤wi≤1051 \le w_i \le 10^5, xi≠yix_i \ne y_i) — the description of the ii-th arc.

It is guaranteed that the lines describe mm arcs of a directed acyclic graph without multiple arcs between the same pair of vertices.

第一行包含两个整数 nn 和 mm(2≤n≤182 \le n \le 18;0≤m≤n(n−1)20 \le m \le \dfrac{n(n - 1)}{2})。

接下来是 mm 行,其中第 ii 行包含三个整数 xix_i、yiy_i 和 wiw_i(1≤xi,yi≤n1 \le x_i, y_i \le n,1≤wi≤1051 \le w_i \le 10^5,xi≠yix_i \ne y_i),表示第 ii 条有向边的描述。

保证这些行描述了一个有向无环图(DAG)的 mm 条边,且任意一对顶点之间不存在重边。

输出格式

Print nn integers a1a_1, a2a_2, ..., ana_n (0≤av≤1090 \le a_v \le 10^9), which must be written on the vertices so that all bib_i are positive, and the value of the expression ∑i=1mwibi\sum \limits_{i = 1}^{m} w_i b_i is the lowest possible. If there are several answers, print any of them. It can be shown that the answer always exists, and at least one of the optimal answers satisfies the constraints 0≤av≤1090 \le a_v \le 10^9.

输出 nn 个整数 a1a_1, a2a_2, ..., ana_n(满足 0≤av≤1090 \le a_v \le 10^9),这些整数需写在图的顶点上,使得所有 bib_i 均为正数,且表达式 ∑i=1mwibi\sum \limits_{i = 1}^{m} w_i b_i 的值尽可能小。若存在多个满足条件的答案,输出任意一个即可。可以证明:答案一定存在,且至少存在一个最优解满足约束 0≤av≤1090 \le a_v \le 10^9。

输入输出样例

  • 输入#1

    3 2
    2 1 4
    1 3 2

    输出#1

    1 2 0
  • 输入#2

    5 4
    1 2 1
    2 3 1
    1 3 6
    4 5 8

    输出#2

    43 42 41 1337 1336
  • 输入#3

    5 5
    1 2 1
    2 3 1
    3 4 1
    1 5 1
    5 4 10

    输出#3

    4 3 2 1 2

输入解题思路,AI测评打分。不知道怎么写?

首页