Member Single Round Match 461 Round 1 - Division I, Level Two BuildingCities
来源:互联网 发布:商品降价提醒功能源码 编辑:程序博客网 时间:2024/06/06 07:16
最短路问题。建图的时候u,v之间建立一条边,并记录为u,v间最少需要修建的城市数。d[i][j]表示当前在第i个点,经过j个新城市的最短路长为多少。只用dp[i][j]<maxTravel的dp[i][j]去更细答案就可以了。
#include <bits/stdc++.h>#define maxn 55using namespace std;const int INF=1e9;int n,w[maxn][maxn];double d[maxn][3000],dis[maxn][maxn];bool done[maxn][3000];struct node{int u,k;double d;node(int _u=0,int _k=0,double _d=0){u=_u;k=_k;d=_d;}bool operator<(const node &rhs)const{return d>rhs.d;}};priority_queue<node>Q;void Dijkstra(int mx){for(int i=0;i<n;i++) for(int j=0;j<3000;j++) d[i][j]=INF;d[0][0]=0;Q.push(node(0,0,0));int u,k;while(!Q.empty()){node now=Q.top();Q.pop();u=now.u;k=now.k;if(done[u][k])continue;if(now.d>mx)continue;done[u][k]=1;for(int i=0;i<n;i++){if(i==u)continue;if(now.d+dis[u][i]>mx)continue;if(d[i][k+w[u][i]]>now.d+dis[u][i]){d[i][k+w[u][i]]=now.d+dis[u][i];Q.push(node(i,k+w[u][i],d[i][k+w[u][i]]));}}}}class BuildingCities{public: int findMinimumCities(int maxDirect, int maxTravel, vector <int> X, vector <int> Y){n=X.size();for(int i=0;i<n;i++)for(int j=0;j<n;j++){dis[i][j]=sqrt((X[i]-X[j])*(X[i]-X[j])+(Y[i]-Y[j])*(Y[i]-Y[j]));w[i][j]=ceil(dis[i][j]/maxDirect)-1;w[i][j]=max(w[i][j],0);}Dijkstra(maxTravel);int ans=INF;for(int i=0;i<3000;i++){if(d[n-1][i]<=maxTravel-1e-5){ans=i;break;}}if(ans==INF)ans=-1;return ans;}};
0 0
- Member Single Round Match 461 Round 1 - Division I, Level Two BuildingCities
- Member Single Round Match 465 Round 1 - Division I, Level Two GreenWarfareze
- Member Single Round Match 474 Round 1 - Division I, Level Two TreesCount
- Topcoder Single Round Match 453.5 Round 1 - Division I, Level Two TheProduct
- topcoder Single Round Match 457 Round 1 - Division I, Level Two baTheHexagonsDivOne
- Single Round Match 464 Round 1 - Division I, Level Two ColorfulDecoration
- Single Round Match 477 Round 1 - Division I, Level Two PythTriplets
- Single Round Match 480 Round 1 - Division I, Level Two NetworkSecurity
- Single Round Match 479 Round 1 - Division I, Level Two TheAirTripDivOne
- Single Round Match 506 Round 1 - Division I, Level Two SlimeXGrandSlimeAuto
- Single Round Match 524 Round 1 - Division I, Level Two LongestSequence
- Single Round Match 573 Round 1 - Division I, Level Two SkiResorts
- Single Round Match 508 Round 1 - Division I, Level Two YetAnotherORProblem
- Single Round Match 517 Round 1 - Division I, Level Two AdjacentSwaps
- Single Round Match 462 Round 1 - Division I, Level Three WarTransportation
- Single Round Match 459 Round 1 - Division II, Level Three gaiParkAmusement
- Single Round Match 671 Round 1 - Division II, Level Three(状压DP)(略难)
- 2010 TCO Algorithm Online Round 5 - Division I, Level Two LongJourney
- Android应用检测并下载更新新版本的方法
- Ubuntu下安装sublimeSublime Text
- Android开发小问题记录
- 基于collectionviewlayout的瀑布流等
- 第1天 母函数
- Member Single Round Match 461 Round 1 - Division I, Level Two BuildingCities
- UVA - 10716 Evil Straw Warts Live 贪心
- 【2120】数据结构实验之链表五:单链表的拆分
- 输入/输出函数
- 【Web 前端】笔记1
- Android 关于BitmapFactory 的使用
- 代理模式
- fhfehg
- index optimized