Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- # define vt vector
- # define pque priority_queue
- typedef long long ll;typedef long double lld;typedef string str;typedef pair<double, double> dd;
- typedef vt<int> vi;typedef vt<vt<int>> vvi;typedef vt<double> vd;typedef vt<ll> vll;typedef pair<int,int> ii;
- typedef vt<lld> vld;typedef vt<char> vc;typedef vt<str> vs;typedef vt<ii> vii;typedef vt<vii> vvii;
- # define f(i,a,b,c) for(ll i=a;i<b;i+=c)
- # define fd(i,a,b,c) for(ll i=a;i>=b;i-=c)
- # define w(x) while(x--)
- # define ctoi(a) (a-'0')
- # define pb push_back
- # define eb emplace_back
- # define lb lower_bound
- # define ub upper_bound
- # define len(x) x.length()
- # define be(x) x.begin(), x.end()
- # define rbe(x) x.rbegin(), x.rend()
- # define bb binary_search
- # define _(x) cout.precision(x);cout.setf(ios::fixed);
- # define ft first
- # define se second
- const int MAX = 1<<10, MAXN = 101, MAXM = 2e4+1, INF = INT_MAX;
- int n, m;
- int u, v, t, w, a, b, pos1, pos2, ou1, ou2;
- vvii gr1, gr2;
- vi dist1, dist2;
- int dijkstra1(int s, int t){
- if(pos1==1)return gr1[ou1][0].se;
- dist1 = vi(n+1, INF);
- pque<ii, vii, greater<ii>> pq;
- dist1[s] = 0, pq.push(ii(dist1[s], s));
- while(!pq.empty()){
- ii topo = pq.top();pq.pop();
- int d = topo.ft, u = topo.se;
- if(d > dist1[u])continue;
- if(u==t){return dist1[t];}
- //if(u==t)return dist[t];
- for(auto [v, w]:gr1[u]){
- if(dist1[u] + w < dist1[v]){
- dist1[v] = dist1[u] + w;
- //cout<<v<<' '<<dist1[v]<<' '<<w<<'\n';
- pq.push(ii(dist1[v], v));
- }
- }
- }
- return INF;
- }
- int dijkstra2(int s, int t){
- if(pos2==1)return gr2[ou2][0].se;
- dist2 = vi(n+1, INF);
- pque<ii, vii, greater<ii>> pq;
- dist2[s] = 0, pq.push(ii(dist2[s], s));
- while(!pq.empty()){
- ii topo = pq.top();pq.pop();
- int d = topo.ft, u = topo.se;
- if(d > dist2[u])continue;
- if(u==t){return dist2[t];}
- //if(u==t)return dist[t];
- for(auto [v, w]:gr2[u]){
- if(dist2[u] + w < dist2[v]){
- dist2[v] = dist2[u] + w;
- //cout<<v<<' '<<dist2[v]<<' '<<w<<'\n';
- pq.push(ii(dist2[v], v));
- }
- }
- }
- return INF;
- }
- int main(){_(0)
- while(cin>>n>>m){
- //if(m==1){cin>>u>>v>>t>>w;cout<<w<<'\n';}
- if(m==1){cin>>u>>v>>t>>w;cout<<w<<'\n';}
- else{
- pos1 = 0, pos2 = 0;
- gr1 = vvii(n+1), gr2 = vvii(n+1);
- f(i,0,m,1){
- cin>>u>>v>>t>>w;
- if(t == 0)gr1[u].eb(v, w), ++pos1, ou1 = u;
- else gr2[u].eb(v, w), ++pos2, ou2 = u;
- }
- a = dijkstra1(1, n), b = dijkstra2(1, n);
- //cout<<a<<' '<<b<<'\n';
- cout<<min(a, b)<<'\n';
- }
- }
- return 0;}
Add Comment
Please, Sign In to add comment