1 条题解

  • 0
    @ 2025-1-22 12:42:43

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 10010, M = 30010;
    int dist[N], n, m, kk, a[N];
    int h[N], e[M], ne[M], w[M], idx;
    void add(int a, int b, int c) {
    	e[++idx] = b, ne[idx] = h[a], h[a] = idx, w[idx] = c;
    }
    void SPFA(int S) {
    	bool st[N];
    	memset(dist, 0x3f, sizeof dist);
    	queue<int> q;
    	q.push(S);
    	st[S] = 1;
    	dist[S] = 0;
    	while (q.size()) {
    		int t = q.front();
    		q.pop();
    		st[t] = 0;
    		for (int i = h[t]  ; i; i = ne[i]) {
    			int j = e[i];
    			if (dist[j] > dist[t] + w[i]) {
    				dist[j] = dist[t] + w[i];
    				if (!st[j]) {
    					q.push(j);
    					st[j] = 1;
    				}
    			}
    		}
    	}
    }
    int main() {
    	cin >> kk >> n >> m;
    	for (int i = 1; i <= kk; i++)
    		cin >> a[i];
    	while (m--) {
    		int a, b, c;
    		scanf("%d%d%d", &a, &b, &c);
    		add(a, b, c);
    		add(b, a, c);
    	}
    	int sum = 0, ans = 1000000;
    	for (int i = 1; i <= n; i++) {
    		sum = 0;
    		SPFA(i);
    		for (int j = 1; j <= kk; j++)
    			sum += dist[a[j]];
    		ans = min(sum, ans);
    	}
    	cout << ans << endl;
    	return 0;
    }
    

    帮忙看看啥情况

    • 1

    信息

    ID
    5883
    时间
    1000ms
    内存
    128MiB
    难度
    3
    标签
    递交数
    8
    已通过
    4
    上传者