1072. Gas Station (30)
来源:互联网 发布:广州打车软件 编辑:程序博客网 时间:2024/05/05 04:04
开始用FLOY求最短路径,奈何最后个测试点超时,只能再试试DIJ来求,然后过了
需要注意的地方:
首先加油站离最近房子的距离尽可能远,如果有不同解,选路径和最小的那个,还一样选序号最小的一个,当然加油站里所有房子的最小路径都不能超过给的数,代码中贴有FLOY解法和DIJ解法,当然FLOY是超时的
#include<iostream>#include<vector>#include<string>#define INF 0x3f3f3fusing namespace std;int N, M, K, D;bool flag;int add, v;int change_index(string str){ if (str[0] == 'G') return N-1+stoi(string(str, 1)); else return stoi(str) - 1;}string change_name(int i){ if (i < N) return to_string(i+1); else return string("G") + to_string(i + 1 - N);}vector<vector<int>> arc;vector<int> Dis;vector<int> Tem;vector<bool> visited;bool DIJ(int index){ int num = 0; visited[index] = true; Dis[index] = 0; for (int t = 0;t < M + N;t++) if (arc[index][t] != INF) Tem[t] = arc[index][t]; while (num != N) { int temp_min=INF,temp_v; for (int t = 0;t < M + N;t++) if (!visited[t] && Tem[t] < temp_min) { temp_min = Tem[t];temp_v = t; } Dis[temp_v] = temp_min; visited[temp_v] = true; if (temp_v < N) { num++; add +=temp_min; if (temp_min < v) v = temp_min; if (temp_min > D) { flag = false;break; } } for (int t = 0;t < M + N;t++) if (!visited[t] && Tem[t]>temp_min + arc[temp_v][t]) Tem[t] = temp_min + arc[temp_v][t]; } return true;}int main(){ cin >> N >> M >> K >> D; if (N == 0) { printf("No Solution\n"); exit(0); } arc.resize(M + N); for (int t = 0;t < M + N;t++) { arc[t].assign(M + N, INF); arc[t][t] = 0; } for (int t = 0;t < K;t++) { string a, b; int c; cin >> a >> b >> c; arc[change_index(a)][change_index(b)] = arc[change_index(b)][change_index(a)]=c; } //for (int t = 0;t < N + M;t++) // for (int i = 0;i < M + N;i++) // for (int j = 0;j < M + N;j++) // { // if (arc[i][t] + arc[t][j] < arc[i][j]) // arc[i][j] = arc[i][t] + arc[t][j]; // } int min=0, sum=0; int resault=0; for (int t = N;t < M + N;t++) { Dis.assign(M + N, 0); Tem.assign(M + N, INF); flag = true; add = 0; v = INF; visited.assign(M + N, false); DIJ(t); //int add = 0,v=INF; //for (int i = 0;i < N;i++) //{ // if (arc[t][i] > D) { flag = 0;break; } // add += arc[t][i]; // if (arc[t][i] < v) v = arc[t][i]; //} if (flag == false)continue; else if(v>min||(v==min && add<sum)){ sum = add;min = v;resault = t; } } if (resault == 0) cout << "No Solution" << endl; else printf("%s\n%.1lf %.1lf\n", change_name(resault).c_str(), double(min), double(sum) / N);}
0 0
- 1072. Gas Station (30)
- 1072. Gas Station (30)
- 1072. Gas Station (30)
- 1072. Gas Station (30)
- 1072. Gas Station (30)
- 1072. Gas Station (30)
- 1072. Gas Station (30)
- 1072. Gas Station (30)
- 1072. Gas Station (30)
- 1072. Gas Station (30)
- 1072. Gas Station (30)
- 1072. Gas Station (30)
- 1072. Gas Station (30)
- 1072. Gas Station (30)
- 1072. Gas Station (30)
- 1072. Gas Station (30)
- 1072.Gas Station (30)
- 1072. Gas Station (30)
- 港版-已过保 MacBookPro因显示屏涂层脱落去"天才吧"免费换屏过程
- gitbook 放弃search-jieba插件
- MySQL的简单查询语句
- 最大子段和
- TypeScript和JavaScript哪种语言更先进
- 1072. Gas Station (30)
- 开发小记
- 【LeetCode】448. Find All Numbers Disappeared in an Array【E】【77】
- 使用Android Studio向SVN上传新项目
- 1009. Product of Polynomials (25)
- MongoDB的聚合操作
- Eclipse配置JDK的图解步骤
- 【Surface Pro3+Archlinux】基本系统+引导器+Secure Boot
- 【安卓学习之工具学习】软件版本管理工具-Eclipse SVN插件Subclipse