香甜的黄油 Sweet Butter
https://www.luogu.org/problemnew/show/P1828
题解:
C++版本一
题解:Floyd+优化+枚举
/*
*@Author: STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=1000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v,val;
int ans,cnt,flag,temp,sum;
int a[N][N];
int mark[N];
char str;
struct node{};
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
//scanf("%d",&t);
//while(t--){
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=m;i++){
for(int j=1;j<=m;j++){
if(i==j){
a[i][j]=0;
}else{
a[i][j]=INF;
}
}
}
for(int i=1;i<=n;i++){
scanf("%d",&t);
mark[t]++;
}
for(int i=1;i<=k;i++){
scanf("%d%d%d",&u,&v,&val);
a[u][v]=val;//初始化d为邻接矩阵
a[v][u]=val;
}
for(int k=1;k<=m;k++){//floyd三重循环
for(int i=1;i<=m;i++){
for(int j=1;j<i;j++){//灵魂剪枝,双向边算一半图就好了
if(a[i][j]>a[i][k]+a[k][j]){
a[i][j]=a[i][k]+a[k][j];
a[j][i]=a[i][j];//更新另一半
}
}
}
}
ans=INF;
for(int i=1;i<=m;i++){
sum=0;
for(int j=1;j<=m;j++){
sum+=a[i][j]*mark[j];//因为是路径和,所以用mark数组保存奶牛头数
}
ans=min(ans,sum);
}
printf("%d",ans);
//}
#ifdef DEBUG
printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
//cout << "Hello world!" << endl;
return 0;
}