3394: [usaco 2009 Jan] best spot best farm time limit: 1 sec memory limit: 128 MB
Submit: 11 solved: 9
[Submit] [Status] Description Input
Input three integers (p, f c.) in row 1st. then, enter an integer in each line of line F to indicate a farm that Bessie liked. next, input three integers (AI, Bi, Ti) in each line in line C to describe a path.
Output
An integer that meets the requirements of the topic. If there are multiple answers, the output number is the smallest.
Sample input13 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 sample output10hint Source
Silver
Question: Why are there so few questions about sb... If floyed cannot be used, use floyed or Dijkstra for n times and then use the N ^ 2 brute force enumeration... Code:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #include<string>12 #define inf 100000000013 #define maxn 100014 #define maxm 10000015 #define eps 1e-1016 #define ll long long17 #define pa pair<int,int>18 #define for0(i,n) for(int i=0;i<=(n);i++)19 #define for1(i,n) for(int i=1;i<=(n);i++)20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)22 #define mod 100000000723 using namespace std;24 inline int read()25 {26 int x=0,f=1;char ch=getchar();27 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}28 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}29 return x*f;30 }31 struct edge{int go,next,w;}e[2*maxm];32 int n,m,k,s,tot,a[maxn],b[maxn],q[maxn],d[maxn][maxn],head[maxn];33 bool v[maxn];34 void ins(int x,int y,int z)35 {36 e[++tot].go=y;e[tot].w=z;e[tot].next=head[x];head[x]=tot;37 }38 void insert(int x,int y,int z)39 {40 ins(x,y,z);ins(y,x,z);41 }42 void spfa(int s)43 {44 for(int i=1;i<=n;++i) d[s][i]=inf;45 memset(v,0,sizeof(v));46 int l=0,r=1,x,y;q[1]=s;d[s][s]=0;47 while(l!=r)48 {49 x=q[++l];if(l==maxn-1)l=0;v[x]=0;50 for(int i=head[x];i;i=e[i].next)51 if(d[s][x]+e[i].w<d[s][y=e[i].go])52 {53 d[s][y]=d[s][x]+e[i].w;54 if(!v[y]){v[y]=1;q[++r]=y;if(r==maxn-1)r=0;}55 }56 }57 }58 int main()59 {60 freopen("input.txt","r",stdin);61 freopen("output.txt","w",stdout);62 n=read();k=read();m=read();63 for1(i,k)a[i]=read();64 for1(i,m)65 {66 int x=read(),y=read(),z=read();67 insert(x,y,z);68 }69 for1(i,n)spfa(i);70 int ans=1;71 for1(i,n)72 {73 for1(j,k)b[i]+=d[i][a[j]];74 if(b[i]<b[ans])ans=i;75 }76 printf("%d\n",ans);77 return 0;78 }View code
Bzoj3394: [usaco 2009 Jan] best spot best farm