BZOJ3394: [Usaco2009 Jan]Best Spot 最佳牧場

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   ar   for   

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 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 7Sample Output10HINT Source

Silver

題解:這麼sb的題為什麼這麼少人A。。。floyed過不了就用n次floyed或者dijkstra然後n^2暴力枚舉就好了。。。代碼:
 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: [Usaco2009 Jan]Best Spot 最佳牧場

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.