3 3 0 0 0 3 0 1 3 2 0 0 0 3 0 1 3 1 0 0 0 3 0 1 16 23 30 40 37 52 49 49 52 64 31 62 52 33 42 41 52 41 57 58 62 42 42 57 27 68 43 67 58 48 58 27 37 69
11 111 -1 10111011HintIn case 1, the judge should select (0, 0) and (0, 3) as the oil station which result in the visiting route: 1->3->2->3->1. And the cost is 2^(1-1) + 2^(2-1) = 3.
这题还比较复杂吧。。
思路:就是去寻找最小的消耗,有点类似贪心
贴个代码!
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<queue> #define inf 1<<30 #define N 105 #define Min(a,b) ((a)<(b)?(a):(b)) #define Max(a,b) ((a)>(b)?(a):(b)) #define LL long long using namespace std; struct Point { int x,y; }p[N]; int n,d; int path[N][N]; int ok[N]; double dist(int i,int j) { return sqrt((double)(p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y)); } bool bfs() { bool vis[N]; int dist[N]; queue<int>que; memset(vis, 0, sizeof(vis)); for(int i=0;i<n;i++) { if(ok[i]) dist[i]=0; else dist[i]=inf; } que.push(0);vis[0]=true; while(!que.empty()) { int u=que.front(); que.pop(); for(int i=0;i<n;i++) { if(!vis[i]&&path[u][i]<=d) { dist[i]=min(dist[i],dist[u]+path[u][i]); if(ok[i]) { que.push(i); vis[i]=true; } } } } for(int i=0;i<n;i++) { if(ok[i]&&!vis[i]) return false; if(!ok[i]&&dist[i]*2>d) return false; } return true; } void fun() { for(int i=0;i<n;i++) ok[i]=1; if(!bfs()) {puts("-1");return; } for(int i=n-1;i>0;i--) { ok[i]=0; if(!bfs()) ok[i]=1; } int j=n-1; while(!ok[j]) j--; for(int i=j;i>=0;i--) printf("%d",ok[i]); putchar(10); } int main() { while(scanf("%d%d",&n,&d)!=EOF) { for(int i=0;i<n;i++) scanf("%d%d",&p[i].x,&p[i].y); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { path[i][j]=ceil(dist(i,j)); } } fun(); } return 0; }