Submission #46596080
Source Code Expand
#include <bits/stdc++.h>
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
using int64 = long long;
using uint = unsigned int;
using uint64 = unsigned long long;
bool ckmin(auto& a, auto b) { return b < a ? a = b, 1 : 0; }
bool ckmax(auto& a, auto b) { return b > a ? a = b, 1 : 0; }
using namespace std;
struct EdgeBase {
int u, v;
operator std::string() {
return "Edge(" + std::to_string(u) + ", " + std::to_string(v) + ")";
}
};
template <typename Graph>
struct GraphNeighborsRange {
struct Iterator {
Graph* g;
int u, id;
bool is_directed;
Iterator(Graph* g_, int u_, int id_) : g(g_), u(u_), id(id_) {
is_directed = g->IsDirected();
}
Iterator& operator++() {
id++;
return *this;
}
bool operator!=(const Iterator& other) { return id != other.id; }
int operator*() {
assert(id < (int)g->go[u].size());
auto& e = g->edges[g->go[u][id]];
if (is_directed) {
return e.v;
} else {
return u ^ e.u ^ e.v;
}
}
};
Graph* g;
int u;
GraphNeighborsRange(Graph* g_, int u_) : g(g_), u(u_) {
assert(0 <= u && u < g->n);
}
Iterator begin() { return Iterator(g, u, 0); }
Iterator end() { return Iterator(g, u, g->go[u].size()); }
};
template <typename Graph>
struct GraphEdgesRange {
struct Iterator {
Graph* g;
int u, id;
Iterator(Graph* g_, int u_, int id_) : g(g_), u(u_), id(id_) {}
Iterator& operator++() {
id++;
return *this;
}
bool operator!=(const Iterator& other) { return id != other.id; }
typename Graph::edge_t& operator*() {
assert(id < (int)g->go[u].size());
return g->edges[g->go[u][id]];
}
};
Graph* g;
int u;
GraphEdgesRange(Graph* g_, int u_) : g(g_), u(u_) {
assert(0 <= u && u < g->n);
}
Iterator begin() { return Iterator(g, u, 0); }
Iterator end() { return Iterator(g, u, g->go[u].size()); }
};
template <typename Edge>
struct Graph {
using edge_t = Edge;
int n;
std::vector<Edge> edges;
std::vector<std::vector<int>> go;
Graph(int n_) : n(n_) { go.resize(n); }
virtual bool IsDirected() const { assert(0); }
virtual void AddEdge(Edge e) = 0;
GraphNeighborsRange<Graph> Neighbors(int u) {
return GraphNeighborsRange<Graph>(this, u);
}
GraphEdgesRange<Graph> Edges(int u) {
return GraphEdgesRange<Graph>(this, u);
}
};
template <typename Edge>
struct UndirectedGraph : public Graph<Edge> {
UndirectedGraph(int n_) : Graph<Edge>(n_) {}
virtual bool IsDirected() const { return false; }
virtual void AddEdge(Edge e) {
assert(e.u >= 0 && e.u < this->n && e.v >= 0 && e.v < this->n);
this->edges.push_back(e);
this->go[e.u].push_back(this->edges.size() - 1);
this->go[e.v].push_back(this->edges.size() - 1);
}
};
template <typename Edge>
struct DirectedGraph : public Graph<Edge> {
DirectedGraph(int n_) : Graph<Edge>(n_) {}
virtual bool IsDirected() const { return true; }
virtual void AddEdge(Edge e) {
assert(e.u >= 0 && e.u < this->n && e.v >= 0 && e.v < this->n);
this->edges.push_back(e);
this->go[e.u].push_back(this->edges.size() - 1);
}
};
template <typename Edge>
struct FlowGraph : public DirectedGraph<Edge> {
using flow_t = decltype(std::declval<Edge>().cap);
FlowGraph(int n_) : DirectedGraph<Edge>(n_) {}
static const flow_t eps = (flow_t)1e-7;
virtual void AddEdge(Edge) { assert(false); }
void AddFlowEdge(Edge e) {
DirectedGraph<Edge>::AddEdge(e);
std::swap(e.u, e.v);
e.cap = 0;
DirectedGraph<Edge>::AddEdge(e);
}
void AddFlowEdgeWithCost(Edge e) {
DirectedGraph<Edge>::AddEdge(e);
std::swap(e.u, e.v);
e.cap = 0;
e.cost *= -1;
DirectedGraph<Edge>::AddEdge(e);
}
};
using flt = double;
struct Edge {
int u, v, b, c;
flt w;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
DirectedGraph<Edge> graph(n);
for (int i = 0; i < m; i++) {
int u, v, b, c;
cin >> u >> v >> b >> c;
u--, v--;
graph.AddEdge({u, v, b, c});
}
auto Check = [&](flt k) {
for (auto& e : graph.edges) {
e.w = e.b - e.c * k;
}
vector<flt> dp(n, -2e9);
dp[n - 1] = 0;
for (int i = n - 2; i >= 0; i--) {
for (auto& e : graph.Edges(i)) {
ckmax(dp[i], dp[e.v] + e.w);
}
}
return dp[0] > 0;
};
flt l = 0, r = 2e9;
for (int i = 0; i < 100; i++) {
flt mid = (l + r) / 2;
if (Check(mid)) {
l = mid;
} else {
r = mid;
}
}
cout << fixed << setprecision(10) << l << '\n';
return 0;
}
Submission Info
Submission Time |
|
Task |
F - Beautiful Path |
User |
xindubawukong |
Language |
C++ 20 (gcc 12.2) |
Score |
500 |
Code Size |
4783 Byte |
Status |
AC |
Exec Time |
530 ms |
Memory |
20336 KiB |
Compile Error
Main.cpp: In function ‘int main()’:
Main.cpp:158:18: warning: missing initializer for member ‘Edge::w’ [-Wmissing-field-initializers]
158 | graph.AddEdge({u, v, b, c});
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
500 / 500 |
Status |
|
|
Set Name |
Test Cases |
Sample |
example0.txt, example1.txt, example2.txt |
All |
000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, 044.txt, 045.txt, 046.txt, 047.txt, 048.txt, 049.txt, 050.txt, 051.txt, 052.txt, example0.txt, example1.txt, example2.txt |
Case Name |
Status |
Exec Time |
Memory |
000.txt |
AC |
334 ms |
20320 KiB |
001.txt |
AC |
271 ms |
20276 KiB |
002.txt |
AC |
71 ms |
10200 KiB |
003.txt |
AC |
113 ms |
9844 KiB |
004.txt |
AC |
109 ms |
10168 KiB |
005.txt |
AC |
126 ms |
10260 KiB |
006.txt |
AC |
197 ms |
10988 KiB |
007.txt |
AC |
293 ms |
20336 KiB |
008.txt |
AC |
71 ms |
10216 KiB |
009.txt |
AC |
72 ms |
10088 KiB |
010.txt |
AC |
112 ms |
10236 KiB |
011.txt |
AC |
133 ms |
15908 KiB |
012.txt |
AC |
313 ms |
20292 KiB |
013.txt |
AC |
180 ms |
14356 KiB |
014.txt |
AC |
306 ms |
20196 KiB |
015.txt |
AC |
350 ms |
20284 KiB |
016.txt |
AC |
417 ms |
18804 KiB |
017.txt |
AC |
460 ms |
18292 KiB |
018.txt |
AC |
493 ms |
17940 KiB |
019.txt |
AC |
519 ms |
17944 KiB |
020.txt |
AC |
515 ms |
17860 KiB |
021.txt |
AC |
488 ms |
17928 KiB |
022.txt |
AC |
530 ms |
17908 KiB |
023.txt |
AC |
522 ms |
17868 KiB |
024.txt |
AC |
474 ms |
17796 KiB |
025.txt |
AC |
434 ms |
17940 KiB |
026.txt |
AC |
450 ms |
17920 KiB |
027.txt |
AC |
414 ms |
17952 KiB |
028.txt |
AC |
449 ms |
17920 KiB |
029.txt |
AC |
460 ms |
17848 KiB |
030.txt |
AC |
457 ms |
17964 KiB |
031.txt |
AC |
512 ms |
17860 KiB |
032.txt |
AC |
493 ms |
17676 KiB |
033.txt |
AC |
455 ms |
17152 KiB |
034.txt |
AC |
327 ms |
20268 KiB |
035.txt |
AC |
336 ms |
18884 KiB |
036.txt |
AC |
396 ms |
18352 KiB |
037.txt |
AC |
493 ms |
17908 KiB |
038.txt |
AC |
462 ms |
17872 KiB |
039.txt |
AC |
460 ms |
17876 KiB |
040.txt |
AC |
465 ms |
17872 KiB |
041.txt |
AC |
467 ms |
17920 KiB |
042.txt |
AC |
456 ms |
17840 KiB |
043.txt |
AC |
464 ms |
17860 KiB |
044.txt |
AC |
511 ms |
17900 KiB |
045.txt |
AC |
444 ms |
17864 KiB |
046.txt |
AC |
453 ms |
17888 KiB |
047.txt |
AC |
396 ms |
17904 KiB |
048.txt |
AC |
431 ms |
17920 KiB |
049.txt |
AC |
394 ms |
17824 KiB |
050.txt |
AC |
466 ms |
17792 KiB |
051.txt |
AC |
436 ms |
17728 KiB |
052.txt |
AC |
396 ms |
17120 KiB |
example0.txt |
AC |
1 ms |
3868 KiB |
example1.txt |
AC |
1 ms |
3752 KiB |
example2.txt |
AC |
1 ms |
3748 KiB |