比赛 寒假集训5 评测结果 AWATTTTTTTTTTTTTTTTT
题目名称 魔法森林 最终得分 10
用户昵称 彭欣越 运行时间 35.719 s
代码语言 C++ 内存使用 6.43 MiB
提交时间 2026-03-01 12:58:10
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100010;
ll n,m,ans1=1e9,ans2=1e9;
int head[N],tot,mk[N];
struct edge {
    int v,nxt;
    ll w1,w2;
}e[N*2];
void add (int u,int v,ll w1,ll w2) {
    e[++tot].v=v;
    e[tot].w1=w1;
    e[tot].w2=w2;
    e[tot].nxt=head[u];
    head[u]=tot;
}
void dfs (int u,ll sum1,ll sum2) {
    if (u==n) {
        ans1=min(ans1,sum1);
        ans2=min(ans2,sum2);
        return;
    }
    for (int i=head[u];i;i=e[i].nxt) {
        int v=e[i].v;
        if (mk[v]) continue;
        mk[v]=1;
        dfs(v,max(sum1,e[i].w1),max(sum2,e[i].w2));
        mk[v]=0;
    }
}
int main () {
    freopen("magicalforest.in","r",stdin);
    freopen("magicalforest.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0); 
    cin >> n >> m;
    for (int i=1;i<=m;i++) {
        int a,b,x,y;
        cin >> a >> b >> x >> y;
        add(a,b,x,y);
        add(b,a,x,y);
        //in[b]++;
    }
    mk[1]=1;
    dfs(1,0,0);
    if (ans1==1e9&&ans2==1e9) cout << -1 <<endl;
    else cout << ans1+ans2 <<endl;
    return 0; 
}