标签:des style blog http color io os ar for
第1行输入三个整数P,F C.之后F行每行输入一个整数表示一个贝茜喜欢的牧场.之后C行每行输入三个整数ai,bi,Ti,描述一条路.
一个整数,满足题目要求的最佳牧场.如果有多个答案,输出编号最小的
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 1000000000 13 #define maxn 1000 14 #define maxm 100000 15 #define eps 1e-10 16 #define ll long long 17 #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 1000000007 23 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 }
BZOJ3394: [Usaco2009 Jan]Best Spot 最佳牧场
标签:des style blog http color io os ar for
原文地址:http://www.cnblogs.com/zyfzyf/p/4005564.html