2
a,b可以交换c,则a->c,距离c[b],b->c,距离为c[a],再加一个超级源点连向每个点,距离为c[i],然后从源点跑最短路,求出到每个点的最短距离。这个最短距离就是通过交换或不换买到物品i的最小代价。有了这些代价后,枚举得到1的方案, 求最小值即可。
#include<cstdio> #include<map> #include<queue> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<list> #include<set> #include<cmath> using namespace std; const int maxn = 1e4 + 5; const int INF = 1e9; const double eps = 1e-6; typedef unsigned long long ULL; typedef long long LL; typedef pair<LL, LL> P; #define fi first #define se second LL c[maxn]; int n; vector<P> G[maxn]; priority_queue<P> q; LL dis[maxn]; void dij(){ while(!q.empty()) q.pop(); for(int i = 1;i <= n;i++) dis[i] = INF; q.push(P(0, 0)); dis[0] = 0; while(!q.empty()){ P tem = q.top(); q.pop(); LL total = -tem.fi; int pos = tem.se; if(total > dis[pos]) continue; for(int i = 0;i < G[pos].size();i++){ P edge = G[pos][i]; int to = edge.fi; LL der = edge.se; if(total+der < dis[to]){ dis[to] = total + der; q.push(P(-dis[to], to)); } } } } P edge[maxn*10]; int main(){ freopen("dwarf.in", "r", stdin); freopen("dwarf.out", "w", stdout); int m; while(scanf("%d%d", &n, &m) != EOF){ for(int i = 0;i <= n;i++) G[i].clear(); for(int i = 1;i <= n;i++){ scanf("%I64d", &c[i]); G[0].push_back(P(i, c[i])); } int cnt = 0; while(m--){ int to, x, y; scanf("%d%d%d", &to, &x, &y); G[x].push_back(P(to, c[y])); G[y].push_back(P(to, c[x])); if(to == 1){ edge[cnt++] = P(x, y); } } dij(); LL ans = dis[1]; for(int i = 0;i < cnt;i++){ int x = edge[i].fi; int y = edge[i].se; ans = min(ans, dis[x]+dis[y]); } printf("%I64d\n", ans); } return 0; }