이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// Calm down.
// Think three times, code twice.
#include "bits/stdc++.h"
#define forr(_a,_b,_c) for(int _a = (_b); _a <= (_c); ++_a)
#define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < (_c); ++_a)
#define st first
#define nd second
#define ll long long
#define ull unsigned long long
#define pii pair <ll,int>
#define pll pair <ll,ll>
#define piii pair <int,pii>
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"
using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7; // 998244353
const ll oo = 1e18;
struct edge {
int u, c; ll p;
};
struct sth {
int u, c; ll p;
bool operator < (const sth &y) const {
return p < y.p;
}
};
int n, m;
ll dp[N];
map<int, ll> dp2[N], sum[N];
map<int, vector<edge>> a[N];
void to_nho_cau() {
cin >> n >> m;
forr(i, 1, m) {
int u, v, c; ll p;
cin >> u >> v >> c >> p;
a[u][c].pb({v, c, p});
a[v][c].pb({u, c, p});
sum[u][c] += p;
sum[v][c] += p;
}
priority_queue<sth, vector<sth>> q;
memset(dp, 63, sizeof dp);
q.push({1, 0, 0LL}); dp[1] = 0;
while(q.size()) {
int u = q.top().u, c = q.top().c; ll du = q.top().p; q.pop();
if(c) {
if(du > dp2[u][c]) continue;
for(edge e: a[u][c]) {
ll cost = sum[u][c] - e.p + du;
// cout << cost << " " << u << " " << e.u << " 3\n";
if(cost < dp[e.u]) {
dp[e.u] = cost;
q.push({e.u, 0, dp[e.u]});
}
}
} else {
if(du > dp[u]) continue;
for(map<int, vector<edge>>::iterator it = a[u].begin(); it != a[u].end(); it++) {
for(edge e: it->nd) {
ll cost = sum[u][e.c] - e.p + du;
if(cost < dp[e.u]) {
// cout << cost << " " << u << " " << e.u << " 1\n";
dp[e.u] = cost;
q.push({e.u, 0, dp[e.u]});
}
cost = e.p + du;
if(cost < dp[e.u]) {
// cout << cost << " " << u << " " << e.u << " 2\n";
dp[e.u] = cost;
q.push({e.u, 0, dp[e.u]});
}
cost = du;
if(!dp2[e.u].count(e.c)) {
// cout << u << " " << e.u << " " << e.c << " " << cost << "\n";
}
if(!dp2[e.u].count(e.c) || cost < dp2[e.u][e.c]) {
dp2[e.u][e.c] = cost;
q.push({e.u, e.c, cost});
}
}
}
}
}
// cout << dp[2] << "\n";
if(dp[n] < oo) {
cout << dp[n] << "\n";
} else cout << -1 << "\n";
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
freopen(file".inp","r",stdin);
freopen(file".out","w",stdout);
int t = 1;
// cin >> t;
while(t--) to_nho_cau();
}
/*
1.self check:
2.long long
3.size of array
4.code for testing
5.initializing
6.modulo number
*/
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen(file".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(file".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |