国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

1018. Public Bike Management (30)

2019-11-14 09:36:16
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

有個(gè)坑:不能把后面多出來(lái)的自行車(chē)補(bǔ)到前面缺的地方,可用DFS或DIJ DFS方法

#include<iostream>#include<vector>#define INF 0x3f3f3f#define MAX_bike 102#define MAX_V 502using namespace std;int C, N, sp, M;int t_min=INF;int send=0, back=0;vector<int> path;//輸出變量int temp_send=0, temp_back=0,temp_min=0;vector<int> temp_path;//臨時(shí)變量int arc[MAX_V][MAX_V] = {0};//鄰接矩陣int bike[MAX_V];//頂點(diǎn)自行車(chē)數(shù)量bool visited[MAX_V];//bfs中是否被訪問(wèn)void intipath()//對(duì)路徑求temp_back和temp_send { int temp; temp = 0, temp_send = 0; for (auto x : temp_path) { if (bike[x] + temp < 0) { temp_send += -(bike[x] + temp);temp = 0; } else temp = bike[x] + temp; } temp_back = temp; }void bfs(int index)//bfs{ if (temp_min > t_min) { return; }//剪枝 if (index == sp) { intipath(); if (temp_min < t_min || (temp_min==t_min && temp_send<send)||(temp_min==t_min && temp_send==send && temp_back<back) ) { t_min = temp_min;send = temp_send;back = temp_back;path = temp_path; } return; } for (int t = 1;t <= N;t++) { if (visited[t] == false && arc[index][t] != 0) { visited[t] = true; temp_path.push_back(t); temp_min += arc[index][t]; bfs(t); visited[t] = false;//回溯 temp_path.pop_back(); temp_min -= arc[index][t]; } }}int main(){ cin >> C >> N >> sp >> M; bike[0] = 0; for (int t = 1;t <= N;t++) { cin >> bike[t]; bike[t] -= C / 2; } for (int t = 0;t < M;t++) { int i, j,k; cin >> i >> j >> k; arc[i][j] = arc[j][i]=k; } visited[0] = true; bfs(0); cout << send << " 0"; for (auto it = path.begin();it != path.end();it++) cout << "->" << *it; cout << " " << back << endl; return 0;}

DIJ方法:

#include<iostream>#include<vector>#define MAX_V 502#define MAX_bike 102#define INF 0x3f3f3f//vector保存的路徑不包括0using namespace std;vector<vector<int>> temp_path[MAX_V];//臨時(shí)路徑,求所有最短路徑解int arc[MAX_V][MAX_V] = {0};//鄰接矩陣int bike[MAX_V];//頂點(diǎn)自行車(chē)數(shù)量int D[MAX_V] = {0};//最短路徑int temp_D[MAX_V];//DIJ臨時(shí)變量int C, N, sp, M;int temp_send, temp_back;vector<int> path;int send=INF, back;//最后要輸出的void DIJ(){ for (int t = 1;t <= N;t++) if (arc[0][t] != 0) temp_D[t] = arc[0][t]; while (D[sp] == 0) { int temp_min=INF, temp_v; for (int t = 1;t <= N;t++) if (D[t]==0 && temp_D[t] < temp_min) { temp_min = temp_D[t]; temp_v = t; } D[temp_v] = temp_min; for (auto &x : temp_path[temp_v]) x.push_back(temp_v); for (int t = 1;t <= N;t++)//更新temp_D { if (D[t] == 0 && arc[temp_v][t]!=0) { if (temp_min + arc[temp_v][t] < temp_D[t]) { temp_D[t] = temp_min + arc[temp_v][t]; temp_path[t] = temp_path[temp_v]; } else if (temp_min + arc[temp_v][t] == temp_D[t]) { temp_path[t].insert(temp_path[t].end(), temp_path[temp_v].begin(), temp_path[temp_v].end()); } } } }}void intipath(vector<int> p){ temp_send = 0; int temp = 0; for (auto x : p) { if (temp + bike[x] < 0) { temp_send += -(temp + bike[x]); temp = 0; } else temp = temp + bike[x]; } temp_back = temp;}int main(){ cin >> C >> N >> sp >> M; vector<int> vec; for (int t = 1;t <= N;t++) { cin >> bike[t]; bike[t] -= C / 2; temp_D[t] = INF;//初始化 temp_path[t].push_back(vec); } for (int t = 0;t < M;t++) { int i, j,k; cin >> i >> j >> k; arc[i][j] = arc[j][i] = k; } DIJ(); for (auto x : temp_path[sp]) { intipath(x); if (temp_send < send || (temp_send == send && temp_back < back)) { path = x; send = temp_send; back = temp_back; } } cout << send << " 0"; for (auto x : path) cout << "->" << x; cout << " " << back << endl;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 富民县| 武夷山市| 大安市| 镇宁| 贵南县| 达拉特旗| 文水县| 潜江市| 龙山县| 三亚市| 沽源县| 乌海市| 米林县| 全州县| 响水县| 凉城县| 永登县| 洞口县| 峨边| 库伦旗| 会同县| 福清市| 陇西县| 中卫市| 桐庐县| 平遥县| 临猗县| 盱眙县| 新民市| 依安县| 抚宁县| 濮阳市| 清徐县| 天全县| 长泰县| 资兴市| 通州市| 徐汇区| 黑龙江省| 福鼎市| 吉木乃县|