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

首頁 > 學院 > 開發設計 > 正文

1018. Public Bike Management (30)

2019-11-14 10:14:32
字體:
來源:轉載
供稿:網友

有個坑:不能把后面多出來的自行車補到前面缺的地方,可用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;//臨時變量int arc[MAX_V][MAX_V] = {0};//鄰接矩陣int bike[MAX_V];//頂點自行車數量bool visited[MAX_V];//bfs中是否被訪問void intipath()//對路徑求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];//臨時路徑,求所有最短路徑解int arc[MAX_V][MAX_V] = {0};//鄰接矩陣int bike[MAX_V];//頂點自行車數量int D[MAX_V] = {0};//最短路徑int temp_D[MAX_V];//DIJ臨時變量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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 麦盖提县| 长海县| 工布江达县| 曲水县| 侯马市| 南部县| 石阡县| 寿阳县| 长沙市| 秦皇岛市| 惠来县| 万州区| 定襄县| 丰镇市| 天台县| 汝州市| 宜春市| 永修县| 阿尔山市| 高邑县| 绥德县| 神农架林区| 永善县| 盘锦市| 仁寿县| 鹿泉市| 万年县| 凉山| 即墨市| 苍南县| 镶黄旗| 乳源| 山东| 民勤县| 浪卡子县| 瓮安县| 平阳县| 谷城县| 新晃| 东丽区| 北安市|