This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
struct edge { int v,c; LLI p; };
bool comp(edge a,edge b) {
return a.c < b.c;
}
vector<edge> adjList[100000],adjList2[100000];
map<pii,LLI> dist;
priority_queue<pair<LLI,pii> > H;
int main() {
int i;
int N,M;
int A,B,C,P;
scanf("%d %d",&N,&M);
for (i = 0; i < M; i++) {
scanf("%d %d %d %d",&A,&B,&C,&P);
A--,B--,C--;
adjList[A].pb((edge){B,C,P});
adjList[B].pb((edge){A,C,P});
}
int j,k;
for (i = 0; i < N; i++) {
sort(adjList[i].begin(),adjList[i].end(),comp);
j = 0;
while (j < adjList[i].size()) {
k = j;
LLI sum = 0;
while ((k < adjList[i].size()) && (adjList[i][j].c == adjList[i][k].c)) sum += adjList[i][k].p,k++;
int e = k;
for (k = j; k < e; k++) adjList2[i].pb((edge){adjList[i][k].v,adjList[i][k].c,sum-adjList[i][k].p});
j = e;
}
}
dist[mp(0,-1)] = 0,H.push(mp(0,mp(0,-1)));
while (!H.empty()) {
LLI d = -H.top().first;
int u = H.top().second.first,c = H.top().second.second;
H.pop();
if (d > dist[mp(u,c)]) continue;
if (c == -1) {
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i].v;
if (!dist.count(mp(v,-1)) || (d+adjList[u][i].p < dist[mp(v,-1)])) {
dist[mp(v,-1)] = d+adjList[u][i].p;
H.push(mp(-dist[mp(v,-1)],mp(v,-1)));
}
}
for (i = 0; i < adjList2[u].size(); i++) {
int v = adjList2[u][i].v,c2 = adjList2[u][i].c;
if (!dist.count(mp(v,-1)) || (d+adjList2[u][i].p < dist[mp(v,-1)])) {
dist[mp(v,-1)] = d+adjList2[u][i].p;
H.push(mp(-dist[mp(v,-1)],mp(v,-1)));
}
if (!dist.count(mp(v,c2)) || (d < dist[mp(v,c2)])) {
dist[mp(v,c2)] = d;
H.push(mp(-dist[mp(v,c2)],mp(v,c2)));
}
}
}
else {
i = lower_bound(adjList2[u].begin(),adjList2[u].end(),(edge){-1,c,-1},comp)-adjList2[u].begin();
for (; (i < adjList2[u].size()) && (adjList2[u][i].c == c); i++) {
int v = adjList2[u][i].v;
if (!dist.count(mp(v,-1)) || (d+adjList2[u][i].p < dist[mp(v,-1)])) {
dist[mp(v,-1)] = d+adjList2[u][i].p;
H.push(mp(-dist[mp(v,-1)],mp(v,-1)));
}
}
}
}
if (!dist.count(mp(N-1,-1))) printf("-1\n");
else printf("%lld\n",dist[mp(N-1,-1)]);
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:81:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | while (j < adjList[i].size()) {
| ~~^~~~~~~~~~~~~~~~~~~
Main.cpp:84:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | while ((k < adjList[i].size()) && (adjList[i][j].c == adjList[i][k].c)) sum += adjList[i][k].p,k++;
| ~~^~~~~~~~~~~~~~~~~~~
Main.cpp:98:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for (i = 0; i < adjList[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
Main.cpp:105:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for (i = 0; i < adjList2[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:119:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
119 | for (; (i < adjList2[u].size()) && (adjList2[u][i].c == c); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
69 | scanf("%d %d",&N,&M);
| ~~~~~^~~~~~~~~~~~~~~
Main.cpp:71:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
71 | scanf("%d %d %d %d",&A,&B,&C,&P);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |