分析
最短路模板,dijkstra+堆优化或者 spfa 都可以做,floyd 就别来啦
枚举每一个可能作为放糖的牧场,然后最短路跑一遍求出每个牧场到该牧场的最短距离,累加,打擂台,得出答案
我一直被卡在建边上,之前偷懒用的矩阵存储,一直TLE,后来改回邻接表,分分钟AC,所以啊建边一定要用邻接表,血的教训啊
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<algorithm>
#define P 802
#define N 3000
#define inf 0x3f3f3f3f
#define in read()
using namespace std;
int n,p,c;
int b[P];
int nxt[N],head[P],w[N],to[N],cnt=0;
void add(int x,int y,int z){
nxt[++cnt]=head[x];head[x]=cnt;w[cnt]=z;to[cnt]=y;
}
priority_queue<pair<int,int> > q;
int d[P];
int ans=0x3f3f3f3f,res;
inline int read(){
char ch;
int f=1,res=0;
while((ch=getchar())<'0'||ch>'9')
if(ch=='-') f=-1;
while(ch>='0'&&ch<='9'