This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 987654321987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a));
#define MEM_1(a) memset((a),-1,sizeof(a));
#define ALL(a) a.begin(),a.end()
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<ld, ld> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<double> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 998244353;
ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; }
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll a, ll b) { if (a == 0 || b == 0)return a + b; return a*(b / gcd(a, b)); }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
Vi v[2][201];
ll dist[2][201],rdist[2][201],tmp[2][201],rtmp[2][201];
int dpre[2][201],rdpre[2][201],tpre[2][201],rtpre[2][201];
bool chk[201],NO[50005];
int x[50001],y[50001];
ll c[50001],d[50001];
int n,m;
void dij(int rev,int S,ll d[2][201],int pre[2][201])
{
fup(i,1,n,1)d[rev][i]=INF,pre[rev][i]=-1,chk[i]=0;
priority_queue<Pll> q;
d[rev][S]=0;
q.push(mp(0,S));
while(!q.empty())
{
int N=q.top().Y;
q.pop();
if(chk[N])continue;
chk[N]=1;
for(int i:v[rev][N])
{
if(NO[i])continue;
int xx=x[i],yy=y[i];
ll cc=c[i];
if(rev)swap(xx,yy);
if(d[rev][yy]>d[rev][N]+cc)
{
d[rev][yy]=d[rev][N]+cc;
pre[rev][yy]=i;
q.push(mp(-d[rev][yy],yy));
}
}
}
}
int main() {
scanf("%d%d",&n,&m);
fup(i,0,m-1,1)
{
scanf("%d%d%lld%lld",x+i,y+i,c+i,d+i);
v[0][x[i]].pb(i);
v[1][y[i]].pb(i);
}
dij(0,1,dist,dpre);dij(0,n,rdist,rdpre);
dij(1,1,dist,dpre);dij(1,n,rdist,rdpre);
ll ans=dist[0][n]+rdist[0][1];
fup(N,1,n,1)
{
for(int i:v[0][N])
{
int xx=x[i],yy=y[i];
ll cc=c[i],dd=d[i];
if((dpre[0][yy]==i || rdpre[0][yy]==i) && (dist[0][xx]+cc+rdist[1][yy]==dist[0][n] || rdist[0][xx]+cc+dist[1][xx]==rdist[0][1]))
{
NO[i]=1;
x[m]=yy,y[m]=xx,c[m]=cc;
v[0][yy].pb(m);
dij(0,1,tmp,tpre);
dij(0,n,rtmp,rtpre);
v[0][yy].pop_back();
NO[i]=0;
ans=min(ans,dd+tmp[0][n]+rtmp[0][1]);
}
else
{
ll tt=dd+min({dist[0][n]+rdist[0][yy]+cc+dist[1][xx], dist[0][yy]+cc+rdist[1][xx]+rdist[0][1], dist[0][yy]+cc+rdist[1][xx]+rdist[0][yy]+cc+dist[1][xx]});
ans=min(ans,tt);
}
}
}
if(ans>=INF)ans=-1;
printf("%lld",ans);
}
Compilation message (stderr)
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:78:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&m);
~~~~~^~~~~~~~~~~~~~
ho_t4.cpp:81:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%lld%lld",x+i,y+i,c+i,d+i);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |