Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- typedef short sh;
- const int MX=1e5+3;
- struct zz
- {
- ll f;
- ll s;
- int th;
- } ;
- int n,m;
- priority_queue<zz> pq;
- map<pair<ll,ll> ,ll> edg[2];
- vector<int> adj[MX][2];
- bool operator<(const zz &x,const zz&y)
- {
- if(x.f!=y.f)return x.f<y.f;
- return x.s<y.s;
- }
- bool vis[MX];
- int main(){
- int t;
- cin>>t;
- while(t--){
- for(int i=0;i<MX;i++){
- adj[i][0].clear();
- adj[i][1].clear();
- vis[i]=0;
- }
- edg[0].clear();
- edg[1].clear();
- scanf("%d%d",&n,&m);
- for(int i=0;i<m;i++)
- {
- int a,b,c,d;
- scanf("%d%d%d%d",&a,&b,&c,&d);
- adj[a][d-1].push_back(b);
- swap(a,b);
- adj[a][d-1].push_back(b);
- if(d==1){
- if(edg[0][{a,b}]!=0)
- if(c>=edg[0][{a,b}])continue;
- edg[0][{a,b}]=edg[0][{b,a}]=c;
- }
- else {
- if(edg[1][{a,b}]!=0)
- if(edg[1][{a,b}]<=c)continue;
- edg[1][{a,b}]=edg[1][{b,a}]=c;
- }
- }
- int h1,h2;
- scanf("%d%d",&h1,&h2);
- zz p={(ll)0,(ll)0,h1};
- pq.push(p);
- pair<ll,ll>ans={1e12,1e12};
- while(!pq.empty())
- {
- zz piece=pq.top();
- pq.pop();
- int x=piece.th;
- ll ldis=-piece.s;
- ll lwalk=-piece.f;
- if(x==h2)
- { // cout<<lwalk<<' '<<ldis<<' '<<x<<endl;;
- pair<ll,ll> pp={lwalk,ldis};
- // cout<<ans.second<<"$$"<<endl;
- ans=min(ans,pp);
- // cout<<ans.second<<"$$"<<endl;
- continue;
- }if(vis[x])continue;
- vis[x]=1;
- for(int v=0;v<2;v++)
- for(int i=0;i<adj[x][v].size();i++)
- {
- int y=adj[x][v][i];
- ll w=edg[v][{x,y}];
- if(w==0)continue;
- w=abs(w);
- //cout<<' '<<v<<' '<<x<<' '<<y<< ' '<<w<<endl;
- pq.push({-1*(lwalk+(1-v)*w),-1*(ldis+w),y});
- }
- }
- while(!pq.empty())
- pq.pop();
- if(ans.first==1e12)
- {
- printf("-1\n");
- continue;
- }
- printf("%I64d %I64d\n",ans.first,ans.second);
- }return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement