首页 > 其他分享 >F - Negative Traveling Salesman

F - Negative Traveling Salesman

时间:2024-01-30 21:57:19浏览次数:28  
标签:weight vertex Negative vertices Sample leq Traveling walk Salesman

F - Negative Traveling Salesman

Problem Statement

There is a weighted simple directed graph with $N$ vertices and $M$ edges. The vertices are numbered $1$ to $N$, and the $i$-th edge has a weight of $W_i$ and extends from vertex $U_i$ to vertex $V_i$. The weights can be negative, but the graph does not contain negative cycles.

Determine whether there is a walk that visits each vertex at least once. If such a walk exists, find the minimum total weight of the edges traversed. If the same edge is traversed multiple times, the weight of that edge is added for each traversal.

Here, "a walk that visits each vertex at least once" is a sequence of vertices $v_1,v_2,\dots,v_k$ that satisfies both of the following conditions:

  • For every $i$ $(1\leq i\leq k-1)$, there is an edge extending from vertex $v_i$ to vertex $v_{i+1}$.
  • For every $j\ (1\leq j\leq N)$, there is $i$ $(1\leq i\leq k)$ such that $v_i=j$.

Constraints

  • $2\leq N \leq 20$
  • $1\leq M \leq N(N-1)$
  • $1\leq U_i,V_i \leq N$
  • $U_i \neq V_i$
  • $(U_i,V_i) \neq (U_j,V_j)$ for $i\neq j$
  • $-10^6\leq W_i \leq 10^6$
  • The given graph does not contain negative cycles.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

$N$ $M$
$U_1$ $V_1$ $W_1$
$U_2$ $V_2$ $W_2$
$\vdots$
$U_M$ $V_M$ $W_M$

Output

If there is a walk that visits each vertex at least once, print the minimum total weight of the edges traversed. Otherwise, print No.


Sample Input 1

3 4
1 2 5
2 1 -3
2 3 -4
3 1 100

Sample Output 1

-2

By following the vertices in the order $2\rightarrow 1\rightarrow 2\rightarrow 3$, you can visit all vertices at least once, and the total weight of the edges traversed is $(-3)+5+(-4)=-2$. This is the minimum.


Sample Input 2

3 2
1 2 0
2 1 0

Sample Output 2

No

There is no walk that visits all vertices at least once.


Sample Input 3

5 9
1 2 -246288
4 5 -222742
3 1 246288
3 4 947824
5 2 -178721
4 3 -947824
5 4 756570
2 5 707902
5 1 36781

Sample Output 3

-449429

 

解题思路

  之前在洛谷上做过类似的题目:P8733 [蓝桥杯 2020 国 C] 补给,所以比赛的时候一下就做出来了。

  看到数据范围容易想到状压 dp,定义 $f(i,j)$ 表示从起点走到当前点 $j$ 且经过的节点所表示的二进制集合为 $i$ 的所有路径中权值的最小值,状态转移方程为 $f(i,j) = \min\limits_{k \in i} \left\{ {f(i \setminus \{k\}, k) + g(k,j)} \right\}$。其中 $g(k,j)$ 指 $k \to j$ 的最短距离,任意两点间的最短路可以通过 Floyd 算法求得。另外 $k \to j$ 所经过的中间节点(不含 $k$ 和 $j$)不会被加入到集合中,这里每次只会加入最后到达的节点。

  为什么这样做是正确的呢?首先上面的做法本质是找到一个 $0 \sim n-1$ 的排列 $p$,使得 $\sum\limits_{i=1}^{n-1}{g(i-1,i)}$ 最小,这条路径必然会经过每个节点至少一次。而最优解所对应的路径中,节点 $0 \sim n-1$ 也必然出现至少一次,对于每个编号的节点从中任选出一个(路径的起点和终点必选),那么整个路径就会被分成 $n-1$ 段,显然每一段的长度必然是对应两端点的最短路径,否则就可以用更短的路径来替换,与最优解矛盾了。

  最后的答案为 $\min\limits_{0 \leq i \leq n-1}\left\{f(2^{n}-1, \, i)\right\}$。

  AC 代码如下,时间复杂度为 $O(n^3 + 2^n \cdot n^2)$:

#include <bits/stdc++.h>
using namespace std;

const int N = 25, M = 1 << 20;

int g[N][N];
int f[M][N];

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    memset(g, 0x3f, sizeof(g));
    for (int i = 1; i <= n; i++) {
        g[i][i] = 0;
    }
    while (m--) {
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        u--, v--;
        g[u][v] = min(g[u][v], w);
    }
    for (int k = 0; k < n; k++) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
            }
        }
    }
    memset(f, 0x3f, sizeof(f));
    for (int i = 0; i < n; i++) {
        f[1 << i][i] = 0;
    }
    for (int i = 1; i < 1 << n; i++) {
        for (int j = 0; j < n; j++) {
            if (i >> j & 1) {
                for (int k = 0; k < n; k++) {
                    if (j == k) continue;
                    if (i >> k & 1) {
                        f[i][j] = min(f[i][j], f[i ^ 1 << j][k] + g[k][j]);
                    }
                }
            }
        }
    }
    int ret = 0x3f3f3f3f;
    for (int i = 0; i < n; i++) {
        ret = min(ret, f[(1 << n) - 1][i]);
    }
    if (ret >= 0x3f3f3f3f >> 1) printf("No");
    else printf("%d", ret);
    
    return 0;
}

  ps:这题还可以建分层图然后跑 spfa,一开始用 std::array<int, 2> 来存二元组结果 TLE 了,然后改成 std::pair<int, int> 刚好卡过去,最慢的数据跑了 $5989 \text{ ms}$,挺哈人的()

  AC 代码如下,最坏情况下的时间复杂度为 $O(2^n \cdot n^3)$:

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> PII;

const int N = 25, M = 1 << 20;

int g[N][N];
int dist[M][N];
bool vis[M][N];

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    memset(g, 0x3f, sizeof(g));
    while (m--) {
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        u--, v--;
        g[u][v] = min(g[u][v], w);
    }
    queue<PII> q;
    memset(dist, 0x3f, sizeof(dist));
    for (int i = 0; i < n; i++) {
        q.push({1 << i, i});
        dist[1 << i][i] = 0;
        vis[1 << i][i] = true;
    }
    while (!q.empty()) {
        auto p = q.front();
        q.pop();
        vis[p.first][p.second] = false;
        for (int i = 0; i < n; i++) {
            if (dist[p.first | 1 << i][i] > dist[p.first][p.second] + g[p.second][i]) {
                dist[p.first | 1 << i][i] = dist[p.first][p.second] + g[p.second][i];
                if (!vis[p.first | 1 << i][i]) {
                    vis[p.first | 1 << i][i] = true;
                    q.push({p.first | 1 << i, i});
                }
            }
        }
    }
    int ret = 0x3f3f3f3f;
    for (int i = 0; i < n; i++) {
        ret = min(ret, dist[(1 << n) - 1][i]);
    }
    if (ret >= 0x3f3f3f3f >> 1) printf("No");
    else printf("%d", ret);
    
    return 0;
}

 

参考资料

  Editorial - AtCoder Beginner Contest 338:https://atcoder.jp/contests/abc338/editorial/9180

标签:weight,vertex,Negative,vertices,Sample,leq,Traveling,walk,Salesman
From: https://www.cnblogs.com/onlyblues/p/17998057

相关文章

  • ABC338 F Negative Traveling Salesman 题解
    QuestionABC338FNegativeTravelingSalesman给出一个\(N\)个点\(M\)条边的有向图,边权可能为负数,但不可能有负环每经过一条边就要加上这条边的代价求,一条路径经过所有的点,并且要求总代价最小Solution观察到\(N\le20\)自然而然想到状压因为多次经过一条边的代价是......
  • Graph regularized non-negative matrix factorization with prior knowledge consist
    Graphregularizednon-negativematrixfactorizationwithpriorknowledgeconsistencyconstraintfordrug-targetinteractionspredictionJunjunZhang 1, MinzhuXie 2 3Affiliations expandPMID: 36581822 PMCID: PMC9798666 DOI: 10.1186/s1285......
  • Graph regularized non-negative matrix factorization with [Formula: see text] nor
    Graphregularizednon-negativematrixfactorizationwith[Formula:seetext]normregularizationtermsfordrug-targetinteractionspredictionJunjunZhang 1, MinzhuXie 2 3Affiliations expandPMID: 37789278 PMCID: PMC10548602 DOI: 10.11......
  • T399742 Ting'er loves traveling 题解
    LinkT399742Ting'erlovestravelingQuestion给出一个图,使得\(1\)到\(N\)的路径上的最大值最小Solution看到最大值最小想到二分,二分最大值\(top\)然后去check验证能不能从\(1\)走到\(N\)Code#include<bits/stdc++.h>#defineintlonglongusingnamespacest......
  • 无涯教程-Dart - isNegative函数
    如果数字为负数,则此属性返回true。isNegative-语法num.isNegativeisNegative-示例voidmain(){intposNum=10;intnegNum=-10;print(posNum.isNegative);print(negNum.isNegative);}它将产生以下输出-falsetrue参考链接https://www.......
  • EndNote 的漫游数据库(Traveling Library)
    漫游数据库简介EndNote在Word中插入文献都是保存为域代码的形式。域代码中包含文献的大多数据信息,也包含漫游数据库中引用的文献。当利用EndNote第一次格式化引文时,EndNote会查找本地数据库的相应的文献。如果再次格式化文献,同样EndNote也会在本地数据库中查找相应的文献,但......
  • Negative controls and Positive controls
    =================Negativecontrolsaregroupswherenophenomenonisexpected.Theyensurethatthereisnoeffectwhenthereshouldbenoeffect.Tocontinuewiththeexampleofdrugtesting,anegativecontrolisagroupthathasnotbeenadministeredt......
  • Travelling Salesman and Special Numbers
    prologue模拟赛的一道题,结果没做出来,丢大人,败大兴。所以过来糊一篇题解。analysis我们看到数据范围这么大,那么肯定不可以一个一个遍历(废话),所以就要考虑这个题目的性质。我们先假设,极端数据\(2^{1000}-1\),这个数字中包含了\(999\)个1(正好感冒了能不能让我喝了)。下一次......
  • 题解 P9695【[GDCPC2023] Traveling in Cells】
    显然,询问的答案即为\(x\)所在的极长的满足颜色均在\(\mathbb{A}\)内的连续段的权值和。如果我们能维护对颜色的单点修改,以及求出某个位置所在极长连续段的左右端点\(l,r\),只需要树状数组即可求出答案。一个朴素的想法是对每种颜色开一棵线段树,单点修改是平凡的,极长连续段左......
  • 连接正负极Connection of positive and negative poles
    不要把物理公式看作数学公式,请也尊重自然的经验法则。Don'tseethephysicalformulasasmathematicalformulas,pleasealsorespecttheexperiencerulesofnature.连接正负极Connectionofpositiveandnegativepoles欧姆定律Ohm'sLaw\(I=\frac{V}{R}\)物理:......