【CCFCSP - 201403-4】无线网络(分层图最短路)
題干:
| 試題編號: | 201403-4 |
| 試題名稱: | 無線網絡 |
| 時間限制: | 1.0s |
| 內存限制: | 256.0MB |
| 問題描述: | 問題描述 目前在一個很大的平面房間里有 n 個無線路由器,每個無線路由器都固定在某個點上。任何兩個無線路由器只要距離不超過 r 就能互相建立網絡連接。 輸入格式 第一行包含四個正整數 n,m,k,r。(2 ≤ n ≤ 100,1 ≤ k ≤ m ≤ 100, 1 ≤ r ≤ 108)。 輸出格式 輸出只有一個數,即在指定的位置中增設 k 個路由器后,從第 1 個路 由器到第 2 個路由器最少經過的中轉路由器的個數。 樣例輸入 5 3 1 3 樣例輸出 2 |
?
?
解題報告:
? ?也寫了個建圖版本的,但是模型建的不太對,會使得可以從虛擬建的回到已經建成的節點,然后再回第一層。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 2e3 + 5; const double eps = 1e-8; const int INF = 0x3f3f3f3f; int n,m,k; ll r; int dis[222][222]; int tot,head[MAX],vis[222][222]; struct Edge {int to,ne;int w; } e[20005];//數組別開小了 void add(int u,int v,double w) {e[++tot].to = v;e[tot].w = w;e[tot].ne = head[u];head[u] = tot; } struct Point {ll x,y; } p[205]; struct Node {int id,kk;Node() {}Node(int id,int kk):id(id),kk(kk) {} }; bool spfa() {memset(dis,INF,sizeof dis);dis[1][0]=0;vis[1][0]=1;queue<Node > q;q.push(Node(1,0));while (!q.empty()) {Node cur=q.front();q.pop();vis[cur.id][cur.kk]=0;for(int i=head[cur.id]; i!=-1; i=e[i].ne) {int nowkk = cur.kk;if(e[i].to > n) nowkk++;if (dis[e[i].to][nowkk]>dis[cur.id][cur.kk]+e[i].w) {dis[e[i].to][nowkk] = dis[cur.id][cur.kk]+e[i].w;if (vis[e[i].to][nowkk]==0) {vis[e[i].to][nowkk]=1;q.push(Node(e[i].to,nowkk));}}}} }int main() {cin>>n>>m>>k>>r;memset(head,-1,sizeof head);for(int i = 1; i<=n+m; i++) {int u,v;scanf("%lld%lld",&p[i].x,&p[i].y);for(int j = 1; j<i; j++) {double dist = (p[i].x-p[j].x) * (p[i].x-p[j].x) + (p[i].y-p[j].y)*(p[i].y-p[j].y) ;if(dist <= r*r) add(i,j,1),add(j,i,1);}}spfa();int ans = INF;for(int i = 0; i<=k; i++) ans = min(ans,dis[2][i]);printf("%d\n",ans-1);return 0 ; } /* 5 3 1 3 0 0 5 5 0 3 0 5 3 5 3 3 4 4 3 0*/?
總結
以上是生活随笔為你收集整理的【CCFCSP - 201403-4】无线网络(分层图最短路)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: srvload.exe - srvloa
- 下一篇: 【2019牛客暑期多校训练营(第一场)