题目: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。。。确实是的
代码:
#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 500 #define maxm 500+100 #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
#define sqr(x) (x)*(x) 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; }
int n,t,k;
struct rec{double x,y;}a[maxn];
double c[maxn][maxn],d[maxn][maxn];
queue<pa>q;
bool v[maxn][maxn]; int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); n=read();t=read();k=read();
for0(i,n)a[i].x=read(),a[i].y=read();
for0(i,n)for0(j,n)c[i][j]=sqrt(sqr(a[i].x-a[j].x)+sqr(a[i].y-a[j].y));
for0(i,n)for0(j,k)d[i][j]=inf;
d[][]=;
q.push(pa(,));
while(!q.empty())
{
int x=q.front().first,y=q.front().second;q.pop();
v[x][y]=;
for1(i,n)
if(i!=x&&d[x][y]+c[x][i]<d[i][y+])
{
d[i][y+]=d[x][y]+c[x][i];
if(!v[i][y+]){v[i][y+]=;q.push(pa(i,y+));}
}
}
double ans=inf;
for1(i,n)if(i!=t)ans=min(ans,d[i][k]+c[i][t]);
printf("%.2f\n",ans); return ; }