好家伙,我的spfa常数这么大?!80分,20T了,求调
查看原帖
好家伙,我的spfa常数这么大?!80分,20T了,求调
546830
XSean楼主2023/5/24 10:34
#include <bits/stdc++.h>

#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define pre(i, a, b) for(int i = (a); i >= (b); i--)
#define Ede(i, u) for(int i = h[u]; i; i = ne[i])
#define go(i, a) for(auto i : a)
//#define int long long
#define LL long long
#define ULL unsigned long long
#define PII pair<int, int>
#define PIL pair<int, long long>
#define PLI pair<long long, int>
#define PLL pair<long long, long long>
#define mp make_pair
#define eb emplace_back
#define opb pop_back
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define sf scanf
#define prf printf
#define el putchar('\n')
#define mms(arr, n) memset(arr, n, sizeof(arr))
#define mmc(arr1, arr2) memcpy(arr1, arr2, sizeof(arr2))
#define Db(x) prf("test(%s): ", x)
const int inf = 0x3f3f3f3f;

template <typename T> inline void rd(T &x){
	x = 0; bool f = true; char ch = getchar();
	while(ch < '0' || ch > '9'){ if(ch == '-') f = false; ch = getchar();}
	while(ch >= '0' && ch <= '9'){ x = (x << 1) + (x << 3) + (ch ^ '0'); ch = getchar();}
	if(!f) x = -x;
}
template <typename T, typename ...Args> inline void rd(T &x, Args &...args){ rd(x); rd(args...);}

using namespace std;

const int N = 1010, M = 2510;
int n, p, c;
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int c){
	e[++idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx;
}
int cow[N];
int dist[N];
bool vis[N];
int q[N * M], hh = 1, tt = 0;
void spfa(int s){
	mms(dist, 0x3f); dist[s] = 0;
	hh = 1, tt = 0;
	q[++tt] = s;
	mms(vis, false); vis[s] = true;
	while(hh <= tt){
		auto u = q[hh++];
		vis[u] = false;
		Ede(i, u){
			int v = e[i];
			if(dist[v] > dist[u] + w[i]){
				dist[v] = dist[u] + w[i];
				if(!vis[v]){
					vis[v] = true;
					q[++tt] = v;
				}
			}
		}
	}
}

int main(){
	/*
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);
	*/
	rd(n, p, c);
	rep(i, 1, n) rd(cow[i]);
	rep(i, 1, c){
		int x, y, z; rd(x, y, z);
		add(x, y, z); add(y, x, z);
	}
	int ans = inf;
	rep(i, 1, p){
		int sum = 0;
		int F = 1;
		spfa(i);
		rep(j, 1, n){
			if(dist[cow[j]] == inf){ F = 0; break;}
			sum += dist[cow[j]];
		}
		if(F) ans = min(ans, sum);
	}
	prf("%d\n", ans);
	return 0;
}





2023/5/24 10:34
加载中...