题意:
你要从西安出发先到上海,然后再到大连,每个城市只能经过一次,问最短路。
题解:
费用流。
找一条从西安到上海的路,再找一条从大连到上海的路,就可以了。
对于从x到y的一条最短路,费用流只要源点连x,汇点连y,流量为1即可。
所以源点连西安容量1, 连大连容量1, 汇点连上海容量2即可,因为每个点只能经过一次,需要拆点。
之所以不能西安到上海,再上海到大连,是因为有可能费用流直接西安到大连,然后上海到上海。
代码:
#include <bits/stdc++.h>
#ifdef LOCAL
#define debug(x) cout<<#x<<" = "<<(x)<<endl;
#else
#define debug(x) 1;
#endif
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
#define lowbit(x) x&-x
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const double PI = acos (-1.);
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e5 + 5;
const int MAXM = 100000;
struct Edge {
int to, next, cap, flow, cost;
} edge[MAXM];
int head[MAXN], tol;
int pre[MAXN], dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从 0~N-1
void init (int n) {
N = n;
tol = 0;
for (int i = 0; i <= n; i++) head[i] = -1;
}
void addedge (int u, int v, int cap, int cost) {
edge[tol].to = v;
edge[tol].cap = cap;
edge[tol].cost = cost;
edge[tol].flow = 0;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = 0;
edge[tol].cost = -cost;
edge[tol].flow = 0;
edge[tol].next = head[v];
head[v] = tol++;
}
bool spfa (int s, int t) {
queue<int>q;
for (int i = 0; i < N; i++) {
dis[i] = INF;
vis[i] = false;
pre[i] = -1;
}
dis[s] = 0;
vis[s] = true;
q.push (s);
while (!q.empty() ) {
int u = q.front();
q.pop();
vis[u] = false;
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (edge[i].cap > edge[i].flow &&
dis[v] > dis[u] + edge[i].cost) {
dis[v] = dis[u] + edge[i].cost;
pre[v] = i;
if (!vis[v]) {
vis[v] = true;
q.push (v);
}
}
}
}
if (pre[t] == -1) return false;
else return true;
}
//返回的是最大流, cost 存的是最小费用
int minCostMaxflow (int s, int t, int &cost) {
int flow = 0;
cost = 0;
while (spfa (s, t) ) {
int Min = INF;
for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]) {
if (Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
}
for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]) {
edge[i].flow += Min;
edge[i ^ 1].flow -= Min;
cost += edge[i].cost * Min;
}
flow += Min;
}
return flow;
}
map<string, int> q;
int main() {
#ifdef LOCAL
freopen ("input.txt", "r", stdin);
#endif
int T;
cin >> T;
while (T--) {
q.clear();
int m;
scanf ("%d", &m);
init (m * 2 + 5);
int tot = 0;
for (int i = 1; i <= m; i++) {
string s, t;
int cost;
cin >> s >> t;
scanf ("%d", &cost);
if (q.find (s) == q.end() ) {
q[s] = tot;
tot += 2;
}
if (q.find (t) == q.end() ) {
q[t] = tot;
tot += 2;
}
addedge (q[s] + 1, q[t], 1, cost);
addedge (q[t] + 1, q[s], 1, cost);
}
int st = tot, en = tot + 1;
for (int i = 0; i <= tot; i += 2)
addedge (i, i + 1, 1, 0);
if (q.find ("Shanghai") == q.end() || q.find ("Xian") == q.end() || q.find ("Dalian") == q.end() ) {
puts ("-1");
continue;
}
//addedge(q["Shanghai"], q["Shanghai"] ^ 1, 1, 0);
addedge (st, q["Dalian"], 1, 0);
addedge (st, q["Xian"], 1, 0);
//addedge(q["Shanghai"] , en, 2, 0);
int cost;
int flow = minCostMaxflow (st, q["Shanghai"], cost);
if (flow == 2) printf ("%d\n", cost);
else puts ("-1");
}
return 0;
}