Beta Round #9 (酱油杯noi考后欢乐赛)PLQ的寻宝

题目:http://www.contesthunter.org/contest/Beta%20Round%20%EF%BC%839%20%28%E9%85%B1%E6%B2%B9%E6%9D%AFnoi%E8%80%83%E5%90%8E%E6%AC%A2%E4%B9%90%E8%B5%9B%29/PLQ%E7%9A%84%E5%AF%BB%E5%AE%9D

题解:看了题目觉得分层图SPFA很好想于是就写了,然后A了。

         看标程发现也可以DP。。。确实是的

代码:

 1 #include<cstdio>
 2 
 3 #include<cstdlib>
 4 
 5 #include<cmath>
 6 
 7 #include<cstring>
 8 
 9 #include<algorithm>
10 
11 #include<iostream>
12 
13 #include<vector>
14 
15 #include<map>
16 
17 #include<set>
18 
19 #include<queue>
20 
21 #include<string>
22 
23 #define inf 1000000000
24 
25 #define maxn 500
26 
27 #define maxm 500+100
28 
29 #define eps 1e-10
30 
31 #define ll long long
32 
33 #define pa pair<int,int>
34 
35 #define for0(i,n) for(int i=0;i<=(n);i++)
36 
37 #define for1(i,n) for(int i=1;i<=(n);i++)
38 
39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
40 
41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
42 
43 #define mod 1000000007
44 #define sqr(x) (x)*(x)
45 
46 using namespace std;
47 
48 inline int read()
49 
50 {
51 
52     int x=0,f=1;char ch=getchar();
53 
54     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
55 
56     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
57 
58     return x*f;
59 
60 }
61 int n,t,k;
62 struct rec{double x,y;}a[maxn];
63 double c[maxn][maxn],d[maxn][maxn];
64 queue<pa>q;
65 bool v[maxn][maxn];
66 
67 int main()
68 
69 {
70 
71     freopen("input.txt","r",stdin);
72 
73     freopen("output.txt","w",stdout);
74 
75     n=read();t=read();k=read();
76     for0(i,n)a[i].x=read(),a[i].y=read();
77     for0(i,n)for0(j,n)c[i][j]=sqrt(sqr(a[i].x-a[j].x)+sqr(a[i].y-a[j].y));
78     for0(i,n)for0(j,k)d[i][j]=inf;
79     d[0][0]=0;
80     q.push(pa(0,0));
81     while(!q.empty())
82     {
83         int x=q.front().first,y=q.front().second;q.pop();
84         v[x][y]=0;
85         for1(i,n)
86          if(i!=x&&d[x][y]+c[x][i]<d[i][y+1])
87          {
88              d[i][y+1]=d[x][y]+c[x][i];
89              if(!v[i][y+1]){v[i][y+1]=1;q.push(pa(i,y+1));}
90          }
91     }
92     double ans=inf;
93     for1(i,n)if(i!=t)ans=min(ans,d[i][k]+c[i][t]);
94     printf("%.2f\n",ans);
95 
96     return 0;
97 
98 } 
View Code

 

posted @ 2014-11-22 10:22  ZYF-ZYF  Views(175)  Comments(0Edit  收藏  举报