#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,p,i; };
bool comp(edge a,edge b) {
return a.c < b.c;
}
vector<edge> adjList[100000];
vi adjList2[100000];
int poss[400000];
LLI dist[100000];
priority_queue<pair<LLI,int> > 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,2*i});
adjList[B].pb((edge){A,C,P,2*i+1});
}
int j;
fill(poss,poss+2*M,-1);
for (i = 0; i < N; i++) {
dist[i] = 1e18;
sort(adjList[i].begin(),adjList[i].end(),comp);
for (j = 0; j < adjList[i].size(); j++) {
if ((j == adjList[i].size()-1) || (adjList[i][j].c != adjList[i][j+1].c)) {
if ((j == 0) || (adjList[i][j].c != adjList[i][j-1].c)) adjList2[i].pb(adjList[i][j].v);
else if ((j == 1) || (adjList[i][j].c != adjList[i][j-2].c)) {
poss[adjList[i][j].i^1] = adjList[i][j-1].v;
poss[adjList[i][j-1].i^1] = adjList[i][j].v;
}
}
}
}
dist[0] = 0,H.push(mp(0,0));
while (!H.empty()) {
LLI d = -H.top().first;
int u = H.top().second;
H.pop();
if (d > dist[u]) continue;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i].v;
if (d+adjList[u][i].p < dist[v]) {
dist[v] = d+adjList[u][i].p;
H.push(mp(-dist[v],v));
}
if (poss[adjList[u][i].i] != -1) {
v = poss[adjList[u][i].i];
if (d < dist[v]) {
dist[v] = d;
H.push(mp(-dist[v],v));
}
}
}
for (i = 0; i < adjList2[u].size(); i++) {
int v = adjList2[u][i];
if (d < dist[v]) {
dist[v] = d;
H.push(mp(-dist[v],v));
}
}
}
if (dist[N-1] == 1e18) printf("-1\n");
else printf("%lld\n",dist[N-1]);
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
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 | for (j = 0; j < adjList[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~~~
Main.cpp:85:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | if ((j == adjList[i].size()-1) || (adjList[i][j].c != adjList[i][j+1].c)) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:101:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | for (i = 0; i < adjList[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
Main.cpp:115:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | for (i = 0; i < adjList2[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:71:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
71 | scanf("%d %d",&N,&M);
| ~~~~~^~~~~~~~~~~~~~~
Main.cpp:73:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
73 | scanf("%d %d %d %d",&A,&B,&C,&P);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Incorrect |
4 ms |
4940 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
80 ms |
12092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Incorrect |
4 ms |
4940 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |