本文主要是介绍acwing1127. 香甜的黄油,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意
思路
暴力 spfa。。。。。
代码
#include <bits/stdc++.h>
using namespace std;
#define db double
#define ll long long
#define Pir pair<int, int>
#define fi first
#define se second
#define pb push_back
#define m_p make_pair
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
/*==========ACMer===========*/
const int N = 805, M = 3000;
int n, p, c;
int a[N];
struct Edge
{int v, w, ne;
} e[M];
int h[N], tot;
void add(int u, int v, int w)
{e[++ tot] = { v, w, h[u] }; h[u] = tot;
}int dis[N], vis[N];void spfa(int s)
{memset(dis, inf, sizeof dis);dis[s] = 0, vis[s] = 1;queue<int> q;q.push(s);while (q.size()){int u = q.front(); q.pop();vis[u] = 0;for (int i = h[u]; i; i = e[i].ne) {int v = e[i].v, w = e[i].w;if (dis[v] > dis[u] + w){dis[v] = dis[u] + w;if (! vis[v]){vis[v] = 1;q.push(v);}}}}
}int main()
{scanf("%d %d %d", &n, &p, &c);for (int i = 1; i <= n; i ++) scanf("%d", &a[i]);int u, v, w;for (int i = 0; i < c; i ++) {scanf("%d %d %d", &u, &v, &w);add(u, v, w);add(v, u, w);}ll ans = inf;for (int i = 1; i <= p; i ++) {spfa(i);ll sum = 0;for (int j = 1; j <= n; j ++) {sum += dis[a[j]];}ans = min(ans, sum);}printf("%lld\n", ans);return 0;
}
这篇关于acwing1127. 香甜的黄油的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!