Untitled
unknown
c_cpp
3 years ago
5.0 kB
9
Indexable
// #include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
struct infor {
int pos;
double x;
double y;
double w;
};
// initial
double customer, satellite;
vector<double> routeList;
vector<double> weightList;
double distanceComputation(infor a, infor b) {
double tmp_x{b.x - a.x};
double tmp_y{b.y - a.y};
double ans{pow(tmp_x, 2) + pow(tmp_y, 2)};
ans = sqrt(ans);
return ans;
}
// v w v w v w v
bool computationOnelevel(vector<int> route, vector<double> w, vector<infor> data) {
double routeDistance{0};
double weight{0};
if (route.empty() || w.empty()) return false;
if (route.begin() != route.end() - 1) return false; // check return to original position
// calculate distance;
for (vector<int>::iterator it = route.begin(); it != route.end() - 1; it++) {
infor a{data.at(*it)};
infor b{data.at(*(it + 1))};
double dis{distanceComputation(a, b)};
routeDistance = routeDistance + dis;
}
routeList.push_back(routeDistance);
// calculate weight
if (w.at(0) > 12500) return false;
int count{0};
while (count < w.size() - 1) {
if ((w.at(count) - w.at(count + 1)) != route.at(count + 1)) return false;
count++;
}
return true;
}
bool computationTwolevel(vector<int> route, vector<double> w, vector<infor> data) {
double routeDistance{0};
double weight{0};
if (route.empty() || w.empty()) return false;
if (route.begin() != route.end() - 1) return false; // check return to original position
// calculate distance;
for (vector<int>::iterator it = route.begin(); it != route.end() - 1; it++) {
infor a{data.at(*it)};
infor b{data.at(*(it + 1))};
double dis{distanceComputation(a, b)};
routeDistance = routeDistance + dis;
}
routeList.push_back(routeDistance);
// calculate weight
if (w.at(0) > 5000) return false;
int count{0};
while (count < w.size() - 1) {
if ((w.at(count) - w.at(count + 1)) != route.at(count + 1)) return false;
count++;
}
return true;
}
int main(int argc, char* argv[]) {
ifstream input(argv[1]);
ifstream correct_output(argv[2]);
ifstream user_output(argv[3]);
// get input
input >> customer;
input >> satellite;
infor tmp;
string line_buffer;
vector<infor> data;
while (getline(input, line_buffer)) {
istringstream iss(line_buffer);
iss >> tmp.pos;
iss >> tmp.x;
iss >> tmp.y;
iss >> tmp.w;
data.push_back(tmp);
}
input.close();
// get optimal solution
double opt;
correct_output >> opt;
correct_output.close();
// get user answer
double ans;
user_output >> ans;
// check one level route
for (int i = 0; i < 3; i++) {
vector<int> onelevelroute;
vector<double> onelevelweight;
string line_buffer;
int tmp;
double tmp_w;
getline(user_output, line_buffer);
istringstream iss(line_buffer);
iss >> tmp;
onelevelroute.push_back(tmp);
while (iss >> tmp_w) {
onelevelweight.push_back(tmp_w);
iss >> tmp;
onelevelroute.push_back(tmp);
}
if (!computationOnelevel(onelevelroute, onelevelweight, data)) {
cout << 0 << '\n';
return 0;
}
}
// check two level route
for (int i = 0; i < 6; i++) {
vector<int> twolevelroute;
vector<double> twolevelweight;
string line_buffer;
int tmp;
getline(user_output, line_buffer);
istringstream iss(line_buffer);
while (iss >> tmp) {
twolevelroute.push_back(tmp);
}
if (!computationTwolevel(twolevelroute, twolevelweight, data)) {
cout << 0 << '\n';
return 0;
}
}
user_output.close();
// check total route
double totalRoute{0};
for (auto v : routeList) {
totalRoute = totalRoute + v;
}
if (totalRoute != ans) {
cout << 0 << '\n';
return 0;
}
cout << ans << ' ' << opt << '\n';
// calculate the score
if (ans / opt > 1.1)
cout << 0 << '\n';
else {
cerr << (ans - opt * 1.1) / (opt - opt * 1.1) * 100 << '\n';
cout << (ans - opt * 1.1) / (opt - opt * 1.1) * 100 << '\n';
}
// float ans,userans;
// user_output >> userans;
// correct_output >> ans;
// cout << userans/ans << '\n';
// output a float number which is in range [0,1]
// this subtask score will be output number multiply score parameters
// score parameters can be set on AdminWebServer
// totol score is the sum of all subtasks score
return 0;
}
Editor is loading...