![BZOJ3394: [Usaco2009 Jan]Best Spot 最佳牧场 BZOJ3394: [Usaco2009 Jan]Best Spot 最佳牧场](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
3394: [Usaco2009 Jan]Best Spot 最佳牧场
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 11 Solved: 9
[Submit][Status]
Description
Input
第1行输入三个整数P,F C.之后F行每行输入一个整数表示一个贝茜喜欢的牧场.之后C行每行输入三个整数ai,bi,Ti,描述一条路.
Output
一个整数,满足题目要求的最佳牧场.如果有多个答案,输出编号最小的
Sample Input
13 6 15
11
13
10
12
8
1
2 4 3
7 11 3
10 11 1
4 13 3
9 10 3
2 3 2
3 5 4
5 9 2
6 7 6
5 6 1
1 2 4
4 5 3
11 12 3
6 10 1
7 8 7
11
13
10
12
8
1
2 4 3
7 11 3
10 11 1
4 13 3
9 10 3
2 3 2
3 5 4
5 9 2
6 7 6
5 6 1
1 2 4
4 5 3
11 12 3
6 10 1
7 8 7
Sample Output
10
HINT
Source
题解:
这么sb的题为什么这么少人A。。。
floyed过不了就用n次floyed或者dijkstra
然后n^2暴力枚举就好了。。。
代码:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 1000
#define maxm 100000
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
struct edge{int go,next,w;}e[*maxm];
int n,m,k,s,tot,a[maxn],b[maxn],q[maxn],d[maxn][maxn],head[maxn];
bool v[maxn];
void ins(int x,int y,int z)
{
e[++tot].go=y;e[tot].w=z;e[tot].next=head[x];head[x]=tot;
}
void insert(int x,int y,int z)
{
ins(x,y,z);ins(y,x,z);
}
void spfa(int s)
{
for(int i=;i<=n;++i) d[s][i]=inf;
memset(v,,sizeof(v));
int l=,r=,x,y;q[]=s;d[s][s]=;
while(l!=r)
{
x=q[++l];if(l==maxn-)l=;v[x]=;
for(int i=head[x];i;i=e[i].next)
if(d[s][x]+e[i].w<d[s][y=e[i].go])
{
d[s][y]=d[s][x]+e[i].w;
if(!v[y]){v[y]=;q[++r]=y;if(r==maxn-)r=;}
}
}
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();k=read();m=read();
for1(i,k)a[i]=read();
for1(i,m)
{
int x=read(),y=read(),z=read();
insert(x,y,z);
}
for1(i,n)spfa(i);
int ans=;
for1(i,n)
{
for1(j,k)b[i]+=d[i][a[j]];
if(b[i]<b[ans])ans=i;
}
printf("%d\n",ans);
return ;
}