日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[codevs 1917] 深海机器人问题

發(fā)布時(shí)間:2025/3/15 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [codevs 1917] 深海机器人问题 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

[codevs 1917] 深海機(jī)器人問題


題解:

看題建圖。

“k個(gè)深海機(jī)器人從(x,y)位置坐標(biāo)出發(fā)”、“r個(gè)深海機(jī)器人可選擇(x,y)位置坐標(biāo)作為目的地”,嗯~暗示已經(jīng)很清楚了。

一開始沒理解題目中關(guān)于地圖大小的敘述,還要記得建立多重邊(有點(diǎn)方格取數(shù)2的味道)。


代碼:


總時(shí)間耗費(fèi): 5ms?
總內(nèi)存耗費(fèi): 364B

#include<cstdio> #include<iostream> #include<vector> #include<queue> #include<algorithm> using namespace std;const int maxn = 500 + 10; const int INF = 1e9 + 7;struct Edge {int from, to, cap, flow, cost; };vector<Edge> edges; vector<int> G[maxn];void AddEdge(int from, int to, int cap, int cost) {edges.push_back((Edge){from, to, cap, 0, cost});edges.push_back((Edge){to, from, 0, 0, -cost});int m = edges.size();G[from].push_back(m-2);G[to].push_back(m-1); }int s, t; int ID[maxn][maxn];int d[maxn*maxn], p[maxn*maxn], a[maxn*maxn]; bool inq[maxn];bool BellmanFord(int& cost) {for(int i = s; i <= t; i++) d[i] = INF;memset(inq, 0, sizeof(inq));d[s] = 0; inq[s] = 1; a[s] = INF; p[s] = 0;queue<int> Q;Q.push(s);while(!Q.empty()) {int x = Q.front(); Q.pop();inq[x] = 0;for(int i = 0; i < G[x].size(); i++) {Edge& e = edges[G[x][i]];if(e.cap > e.flow && d[e.to] > d[x] + e.cost) {d[e.to] = d[x] + e.cost;p[e.to] = G[x][i];a[e.to] = min(a[x], e.cap-e.flow);if(!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; }}}}if(d[t] == INF) return 0;cost += a[t] * d[t];int x = t;while(x != s) {edges[p[x]].flow += a[t];edges[p[x]^1].flow -= a[t];x = edges[p[x]].from;}return 1; }void MincostMaxflow() {int cost = 0;while(BellmanFord(cost));cout << -cost << endl; }int main() {int A, B, m, n;cin >> A >> B >> m >> n; m++; n++; s = 0; t = m*n + 1;for(int x = 1, c = 1; x <= m; x++)for(int y = 1; y <= n; y++, c++)ID[x][y] = c;for(int x = 1; x <= m; x++)for(int y = 1; y < n; y++) {int& from = ID[x][y];int& to = ID[x][y+1];int cost;cin >> cost;AddEdge(from, to, 1, -cost);AddEdge(from, to, INF, 0);}for(int y = 1; y <= n; y++)for(int x = 1; x < m; x++) {int& from = ID[x][y];int& to = ID[x+1][y];int cost;cin >> cost;AddEdge(from, to, 1, -cost);AddEdge(from, to, INF, 0);}for(int i = 1; i <= A; i++) {int k, x, y;cin >> k >> x >> y;AddEdge(s, ID[x+1][y+1], k, 0);}for(int i = 1; i <= B; i++) {int r, x, y;cin >> r >> x >> y;AddEdge(ID[x+1][y+1], t, r, 0);}MincostMaxflow();return 0; }

總結(jié)

以上是生活随笔為你收集整理的[codevs 1917] 深海机器人问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。