Submission #231004

#TimeUsernameProblemLanguageResultExecution timeMemory
231004_7_7_Olympic Bus (JOI20_ho_t4)C++14
0 / 100
44 ms3964 KiB
#include <bits/stdc++.h>                                           
 
#define int long long
//#pragma GCC optimize("Ofast")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
 
 
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define forev(i, b, a) for(int i = (b); i >= (a); --i)
#define forn(i, a, b) for(int i = (a); i <= (b); ++i)
#define all(x) x.begin(), x.end()
#define sz(s) (int)s.size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define s second
#define f first
 
 
using namespace std;
 
 
typedef pair < long long, long long > pll;    
typedef pair < int, int > pii;
typedef unsigned long long ull;         
typedef vector < pii > vpii;
typedef vector < int > vi;
typedef long double ldb;  
typedef long long ll;  
typedef double db;                             
 
 
const int inf = 1e9, maxn = 208, mod = 1e9 + 7, N = 5e4 + 11;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 555;
const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9);
const db eps = 1e-12, pi = 3.14159265359;
const ll INF = 1e18;

int n, m, v[N], u[N], c[N], d[N], p[maxn], Bad, F[maxn][maxn], D[3][maxn];
set < int > path[2];
set < pii > q;
vi g[maxn];


void Floyd () {
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= n; ++j)	
		    if (i != j)
				F[i][j] = INF;

	for (int i = 1; i <= m; ++i)
		F[v[i]][u[i]] = min(F[v[i]][u[i]], c[i]);

	for (int k = 1; k <= n; ++k)
		for (int i = 1; i <= n; ++i)
			for (int j = 1; j <= n; ++j)
				F[i][j] = min(F[i][j], F[i][k] + F[k][j]);

}

void Dijkstra (int s, int f, int j) {
	memset(p, -1, sizeof(p));	

	for (int i = 1; i <= n; ++i)
		D[j][i] = INF;


	D[j][s] = 0;
	q.insert({0, s});
	while (sz(q)) {
		int V = q.begin()->s;
		q.erase(q.begin());
		for (auto i : g[V]) 
			if (i != Bad && D[j][u[i]] > D[j][V] + c[i]) {
				q.erase(mp(D[j][u[i]], u[i]));
				D[j][u[i]] = D[j][V] + c[i];
				p[u[i]] = i;
				q.insert(mp(D[j][u[i]], u[i]));
			}
	}

	if (D[j][f] == INF || j > 1)
		return;
	while (f != s) {
		path[j].insert(p[f]);
		f = v[p[f]];
	}
}


main () {
	scanf("%lld%lld", &n, &m);
	for (int i = 1; i <= m; ++i) {
		scanf("%lld%lld%lld%lld", &v[i], &u[i], &c[i], &d[i]);
		g[v[i]].pb(i);
	}

	Floyd();
	Dijkstra(1, n, 0);
	Dijkstra(n, 1, 1);
	

	int ans = min(INF, D[0][n] + D[1][1]);
	for (int i = 1; i <= m; ++i) {
		if (D[0][n] < INF) {
			int cur;
			if (path[0].count(i)) {
				Bad = i;	
				Dijkstra(1, n, 2);
				cur = D[2][n];
			} else
				cur = D[0][n];

			cur += F[n][u[i]] + c[i] + F[v[i]][1] + d[i];
			ans = min(ans, cur);
		}

		if (D[1][1] < INF) {
			int cur;
			if (path[1].count(i)) {
				Bad = i;	
				Dijkstra(n, 1, 2);
				cur = D[2][1];
			} else
				cur = D[1][1];

			cur += F[1][u[i]] + c[i] + F[v[i]][n] + d[i];
			ans = min(ans, cur);			
		}
	}

	if (ans == INF)
		ans = -1;
	printf("%lld\n", ans);			
}

Compilation message (stderr)

ho_t4.cpp:92:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main () {
       ^
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:93:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld%lld", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~~~~~
ho_t4.cpp:95:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld%lld%lld", &v[i], &u[i], &c[i], &d[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...