제출 #1274314

#제출 시각아이디문제언어결과실행 시간메모리
1274314quan606303Robot (JOI21_ho_t4)C++20
34 / 100
3095 ms53404 KiB
/*
 * @Author: RMQuan 
 * @Date: 2025-09-29 22:50:45 
 * @Last Modified by: RMQuan
 * @Last Modified time: 2025-09-30 02:05:22
 */
/*idea : 



*/
#include <bits/stdc++.h>
bool M1;
#define int long long
#define ll long long
#define INTMAX INT_MAX
#define INTMIN INT_MIN
#define LONGMAX LLONG_MAX
#define LONGMIN LLONG_MIN
#define fi first
#define se second
#define memfull(a,b) memset(a,b,sizeof(a));
#define endl '\n'
#define file(a) freopen(a".inp","r",stdin); freopen(a".out","w",stdout);
using namespace std;
const int MOD=1e9+7;
const int maxn=2e5+7;
const int inf=1e18;
struct canh
{
    int u,v,w,col;
};
vector<canh> adj[maxn];
int n,m;
vector<int> dis[maxn];
vector<int> vst[maxn];
vector<int> avaiable_col[maxn];
struct node
{
    int u,prev_col,dis;  
};
struct cmp
{
    bool operator()(const node &x,const node &y)const{
        return x.dis>y.dis;
    }
};
int cnt_w[maxn],cnt[maxn];
int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    for (int i=1;i<=m;i++)
    {
        int u,v,w,col;
        cin>>u>>v>>col>>w;
        adj[u].push_back({u,v,w,col});
        adj[v].push_back({v,u,w,col});
        avaiable_col[u].push_back(col);
        avaiable_col[v].push_back(col);
    }
    for (int i=1;i<=n;i++)
    {
        avaiable_col[i].push_back(-inf);
        sort(avaiable_col[i].begin(),avaiable_col[i].end());
        avaiable_col[i].erase(unique(avaiable_col[i].begin(),avaiable_col[i].end()),avaiable_col[i].end());
        int sz=avaiable_col[i].size();
        dis[i].assign(sz+5,inf);
        vst[i].assign(sz+5,0);
    }
    priority_queue<node,vector<node>,cmp> q;
    dis[1][0]=0;
    q.push({1,0,0});
    while (q.size())
    {
        int u=q.top().u;
        int col=q.top().prev_col;
        q.pop();
        if (vst[u][col])continue;
        vst[u][col]=true;
        for (auto i:adj[u])
        {
            cnt_w[i.col]+=i.w;
            cnt[i.col]++;
        }
        if (col==0)
        {
            
            for (auto i:adj[u])
            {
                int c=i.col;
                int v=i.v;
                // check th1 :
                if (cnt[c]<=1)
                {
                    if (dis[v][0]>dis[u][col])
                    {
                        dis[v][0]=dis[u][col];
                        q.push({v,0,dis[v][0]});
                    }
                }
                else
                {
                    //th2 :
                    int n_c=lower_bound(avaiable_col[v].begin(),avaiable_col[v].end(),c)-avaiable_col[v].begin();
                    if (dis[v][n_c]>dis[u][col])
                    {
                        dis[v][n_c]=dis[u][col];
                        q.push({v,n_c,dis[v][n_c]});
                    }
                    if (dis[v][0]>dis[u][col]+i.w)
                    {
                        dis[v][0]=dis[u][col]+i.w;
                        q.push({v,0,dis[v][0]});
                    }
                    //th3 :
                    if (dis[v][0]>dis[u][col]+cnt_w[c]-i.w)
                    {
                        dis[v][0]=dis[u][col]+cnt_w[c]-i.w;
                        q.push({v,0,dis[v][0]});
                    }
                }
            }
        }
        else
        {
            int pv_col=avaiable_col[u][col];
            for (auto i:adj[u])
            {
                int c=i.col;
                int v=i.v;
                if (c==pv_col&&v!=u)
                {
                    if (dis[v][0]>dis[u][col]+cnt_w[c]-i.w)
                    {
                        dis[v][0]=dis[u][col]+cnt_w[c]-i.w;
                        q.push({v,0,dis[v][0]});
                    }
                }
            }
        }
        for (auto i:adj[u])
        {
            cnt_w[i.col]-=i.w;
            cnt[i.col]--;
        }
    }
    cout<<(dis[n][0]==inf?-1:dis[n][0]);
    bool M2;
    cerr<<"-------------------------------------------------"<<endl;
    cerr<<"Time : "<<clock()<<" ms"<<endl;
    cerr<<"Memory : "<<abs(&M2-&M1)/1024/1024<<" MB"<<endl;
    cerr<<"-------------------------------------------------"<<endl;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...