原题传送门
直接缩点,缩好后也不用建新图了,数据小
边权取最小值
对于每个新点,往外做一次,综合ans
最后ans*2,因为往返
Code:
#include <bits/stdc++.h>
#define maxn 1010
#define LL long long
using namespace std;
struct Edge{
int to, next;
}edge[maxn << 1];
int num, head[maxn], dfn[maxn], low[maxn], Index, vis[maxn], top, sta[maxn];
int dis[maxn][maxn], n, color[maxn], tot;
inline int read(){
int s = 0, w = 1;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
return s * w;
}
void addedge(int x, int y){ edge[++num] = (Edge) { y, head[x] }; head[x] = num; }
void tarjan(int u){
dfn[u] = low[u] = ++Index;
vis[u] = 1, sta[++top] = u;
for (int i = head[u]; i; i = edge[i].next){
int v = edge[i].to;
if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]); else
if (vis[v]) low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u]){
++tot;
while (sta[top + 1] != u)
color[sta[top]] = tot, vis[sta[top--]] = 0;
}
}
int main(){
n = read();
for (int i = 1; i <= n; ++i){
int x = read(), y = read();
addedge(x, y); addedge(y, x);
}
for (int i = 1; i <= n; ++i)
if (!dfn[i]) tarjan(i);
memset(dis, 0x3f, sizeof(dis));
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j){
int x = read();
dis[color[i]][color[j]] = min(dis[color[i]][color[j]], x);
}
LL ans = 1e9;
for (int i = 1; i <= tot; ++i){
LL sum = 0;
for (int j = 1; j <= tot; ++j)
if (i != j) sum += dis[i][j];
ans = min(ans, sum);
}
printf("%lld\n", ans << 1);
return 0;
}