做运动

题目描述

一天,Y 君在测量体重的时候惊讶的发现,由于常年坐在电脑前认真学习,她的体重有了突飞猛进的增长。

幸好 Y 君现在退役了,她有大量的时间来做运动,她决定每天从教学楼跑到食堂来减肥。

Y 君将学校中的所有地点编号为 1 到 n,其中她的教学楼被编号为 S,她的食堂被编号为 T,学校中有 m 条连接两个点的双向道路,保证从任意一个点可以通过道路到达学校中的所有点。

然而 Y 君不得不面临一个严峻的问题,就是天气十分炎热,如果 Y 君太热了,她就会中暑。

于是 Y 君调查了学校中每条路的温度 t,及通过一条路所需的时间 c。Y 君在温度为 t 的地方跑单位时间,就会使她的热量增加 t。

由于热量过高 Y 君就会中暑,而且 Y 君也希望在温度较低的路上跑,她希望在经过的所有道路中最高温度最低的前提下,使她到达食堂时的热量最低 (从教学楼出发时,Y 君的热量为 0)。

请你帮助 Y 君设计从教学楼到食堂的路线,以满足她的要求。你只需输出你设计的路线中所有道路的最高温度和 Y 君到达食堂时的热量。

输入

第一行由一个空格隔开的两个正整数 n,m,代表学校中的地点数和道路数。

接下来 m 行,每行由一个空格隔开的四个整数 a,b,t,c 分别代表双向道路的两个端点,温度和通过所需时间。

最后一行由一个空格隔开的两个正整数 S,T,代表教学楼和食堂的编号。

注意:输入数据量巨大,请使用快速的读入方式。 n ≤ 5 × 10 5 ,m ≤ 10 6 ,0 ≤ t ≤ 10000,0 ≤ c ≤ 10 8 ,1 ≤ a,b,S,T ≤ n,S≠ T


输出

一行由一个空格隔开的两个整数,分别代表最高温度和热量。


样例输入

5 6
1 2 1 2
2 3 2 2
3 4 3 4
4 5 3 5
1 3 4 1
3 5 3 6
1 5


样例输出

3 24



题解

拿到这题的第一想法:二分+最短路。但仔细想了想,好像会超时。

于是就有了并查集的做法。

把温度排序,按从小到大的顺序放入并查集里,直到S和T连通,此时的温度为最高温度。

把小于等于这个温度的边全部连边,按热量跑最短路即可。

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long

const int maxn=5e5+50;
const int maxm=2e6+50;
const ll inf=1e15;

int n,m,fat[maxn],S,T,x,y,t,cc,w,q[maxn+50];
int fir[maxn],to[maxm],nex[maxm],ecnt;
ll c[maxn],wi[maxm];
bool p[maxn];

struct node{int x,y,t,c;}a[maxm];
int cmp(const node &a,const node &b){return a.t<b.t;}

template<typename T>void read(T& aa){
    char cc; ll ff;aa=0;cc=getchar();ff=1;
    while((cc<'0'||cc>'9')&&cc!='-') cc=getchar();
    if(cc=='-') ff=-1,cc=getchar();
    while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
    aa*=ff;
}

void add_edge(int u,int v,ll w){
    nex[++ecnt]=fir[u];fir[u]=ecnt;to[ecnt]=v;wi[ecnt]=w;
}

int father(int x){
    if(x!=fat[x]) fat[x]=father(fat[x]);
    return fat[x];
}

void un(int x,int y){
    int fa=father(x),fb=father(y);
    if(fa!=fb) fat[fa]=fb;
}

void spfa(int x){
    int head=0,tail=1;
    memset(p,false,sizeof(p));
    for(int i=1;i<=n;i++) c[i]=inf;
    p[x]=true;
    c[x]=0;
    q[1]=x;
    while(head!=tail){
        head++;
        head=(head-1)%maxn+1;
        int u=q[head];
        p[u]=false;
        for(int e=fir[u];e;e=nex[e]){
            int v=to[e];
            if(c[u]+wi[e]<c[v]){
                c[v]=c[u]+wi[e];
                if(!p[v]){
                    tail++;
                    tail=(tail-1)%maxn+1;
                    p[v]=true;
                    q[tail]=v;
                }
            }
        }
    }
}

int main(){
    freopen("running.in","r",stdin);
    freopen("running.out","w",stdout);
    read(n),read(m);
    for(int i=1;i<=n;i++) fat[i]=i;
    for(int i=1;i<=m;i++) read(a[i].x),read(a[i].y),read(a[i].t),read(a[i].c);
    read(S),read(T);
    sort(a+1,a+1+m,cmp);
    for(int i=1;i<=m;i++){
        int fa=father(a[i].x),fb=father(a[i].y);
        if(fa!=fb){
            un(a[i].x,a[i].y);
            if(father(S)==father(T)){
                w=i;break;
            }
        }
    }
    for(int i=1;i<=m;i++)
    if(a[i].t<=a[w].t){
        add_edge(a[i].x,a[i].y,(ll)a[i].t*a[i].c);
        add_edge(a[i].y,a[i].x,(ll)a[i].t*a[i].c);
    }
    spfa(S);
    cout<<a[w].t<<" "<<c[T]<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/rlddd/p/9814329.html