#include <bits/stdc++.h>
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
#define pb push_back
using namespace std;
using ll = long long;
using pii = pair <int, int>;
const int thrs = 4e6;
const int maxN = 2004 + 1;
const ll inf = 0x3f3f3f3f3f3f3f3f;
//const int Mod =
int n, m;
struct TEdge{
ll u, t, c;
};
vector <TEdge> adj[maxN];
struct TPQ{
ll u, t, c;
bool operator < (const TPQ &other) const{
if (c == other.c) return t > other.t;
return c > other.c;
}
};
ll d[maxN];
int t[maxN];
void Init(){
cin >> n >> m;
for (int i = 1; i <= m; ++i){
int u, v, t, c;
cin >> u >> v >> t >> c;
adj[u].pb({v, t, c});
adj[v].pb({u, t, c});
}
priority_queue <TPQ> PQ;
PQ.push({1, 0, 0});
memset(d, 0x3f, sizeof(d));
memset(t, 0x3f, sizeof(t));
d[1] = t[1] = 0;
while (!PQ.empty()){
auto u = PQ.top();
PQ.pop();
d[u.u] = min(d[u.u], u.t * u.c);
if (t[u.u] < u.t) continue;
t[u.u] = u.t;
for (auto i: adj[u.u]){
if (u.t + i.t > thrs) continue;
PQ.push({i.u, u.t + i.t, u.c + i.c});
}
}
for (int i = 2; i <= n; ++i){
if (d[i] == inf){
cout << -1 << "\n";
}
else cout << d[i] << "\n";
}
}
#define taskname "test"
signed main(){
faster
if (fopen(taskname ".inp", "r")){
freopen(taskname ".inp", "r", stdin);
freopen(taskname ".out", "w", stdout);
}
int tt = 1;
//cin >> tt;
while (tt--){
Init();
}
if (fopen("timeout.txt", "r")){
ofstream timeout("timeout.txt");
cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
timeout.close();
}
}
Compilation message
ceste.cpp: In function 'int main()':
ceste.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | freopen(taskname ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ceste.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | freopen(taskname ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
724 KB |
Output is correct |
3 |
Correct |
4 ms |
788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
2028 KB |
Output is correct |
2 |
Correct |
49 ms |
3656 KB |
Output is correct |
3 |
Correct |
63 ms |
3792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
155 ms |
3752 KB |
Output is correct |
2 |
Correct |
584 ms |
25336 KB |
Output is correct |
3 |
Correct |
5 ms |
916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
1452 KB |
Output is correct |
2 |
Correct |
325 ms |
12936 KB |
Output is correct |
3 |
Correct |
9 ms |
756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
196 ms |
1476 KB |
Output is correct |
2 |
Correct |
204 ms |
3772 KB |
Output is correct |
3 |
Correct |
182 ms |
3648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
185 ms |
3660 KB |
Output is correct |
2 |
Correct |
182 ms |
3648 KB |
Output is correct |
3 |
Correct |
256 ms |
3776 KB |
Output is correct |