이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
using namespace std;
const int mxN = 1e3 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
struct item{
ll d;
int pre;
bool spec;
int u;
};
struct cmp{
bool operator()(const item &a , const item &b)
{
return a.d > b.d;
}
};
int n , m;
vector<pair<int , int>> adj[mxN];
int color[mxN];
int cost[mxN];
int cnt[mxN];
ll sumcost[mxN];
ll dist[mxN][mxN][2];
void solve()
{
cin >> n >> m;
for(int i = 1 ; i <= m ; i++) {
int u , v;
cin >> u >> v >> color[i] >> cost[i];
adj[u].pb({v , i});
adj[v].pb({u , i});
}
memset(dist , 0x3f , sizeof(dist));
priority_queue<item , vector<item> , cmp> pq;
pq.push({0 , 0 , 0 , 1});
dist[1][0][0] = 0;
while(!pq.empty()) {
int u = pq.top().u;
ll d = pq.top().d;
int pre = pq.top().pre;
bool spec = pq.top().spec;
pq.pop();
if(d != dist[u][pre][spec]) continue;
for(auto it : adj[u]) {
int v = it.fi;
int c = it.se;
if(v != pre) {
cnt[color[c]]++;
sumcost[color[c]] += cost[c];
}
if(v == pre && spec == false) {
cnt[color[c]]++;
sumcost[color[c]] += cost[c];
}
}
for(auto it : adj[u]) {
int v = it.fi;
int c = it.se;
if(v == pre) continue;
if(cnt[color[c]] == 1) {
if(minimize(dist[v][u][0] , d) == true) {
pq.push({d , u , 0 , v});
}
}
else {
if(minimize(dist[v][u][0] , d + sumcost[color[c]] - cost[c])) {
pq.push({d + sumcost[color[c]] - cost[c] , u , 0 , v});
}
if(minimize(dist[v][u][1] , d + cost[c]) == true) {
pq.push({d + cost[c] , u , 1 , v});
}
}
}
for(auto it : adj[u]) {
int v = it.fi;
int c = it.se;
if(v != pre) {
cnt[color[c]]--;
sumcost[color[c]] -= cost[c];
}
if(v == pre && spec == false) {
cnt[color[c]]--;
sumcost[color[c]] -= cost[c];
}
}
}
ll res = dist[0][0][0];
for(int i = 1 ; i <= n ; i++)
for(int j = 0 ; j < 2 ; j++) minimize(res , dist[n][i][j]);
if(res == dist[0][0][0]) cout << -1;
else cout << res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen(TASK".inp" , "r" , stdin);
//freopen(TASK".out" , "w" , stdout);
int tc = 1;
//cin >> tc;
while(tc--) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |