結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー vjudge1
提出日時 2025-10-01 17:57:49
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 309 ms / 2,000 ms
コード長 1,641 bytes
コンパイル時間 1,625 ms
コンパイル使用メモリ 168,720 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-10-01 17:57:56
合計ジャッジ時間 7,055 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
int vx[4010],nx[4010],to[4010],hd[2010],ed;
int t,n,m,d[2010];
bitset<2010> vis;
struct nd{
    int x,i;
    nd(int _x = 0,int _i = 0){
        x = _x,i = _i;
    }
    bool operator<(const nd &p)const{
        return p.x < x;
    }
};
priority_queue<nd> q;
void ad(int x,int y,int z){
    nx[++ed] = hd[x];
    hd[x] = ed;
    to[ed] = y;
    vx[ed] = z;
    return ;
}
inline int dj(int ss,int tt){
    for(int i = 1;i <= n;++i)
        d[i] = 1e15 + 7,vis[i] = false;
    d[ss] = 0;
    q.push(nd(0,ss));
    while(!q.empty()){
        int u = q.top().i;
        q.pop();
        if(vis[u])continue;
        vis[u] = true;
        for(int i = hd[u];i;i = nx[i]){
            int y = to[i],v = vx[i];
            if(d[y] > d[u] + v){
                d[y] = d[u] + v;
                if(!vis[y])q.push(nd(d[y],y));
            }
        }
    }
    return d[tt];
}
signed main(){
    // freopen("a.in","r",stdin);
    // freopen("a.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>t>>n>>m;
    ed = 1;
    for(int i = 1;i <= m;++i){
        int x,y,z;
        cin>>x>>y>>z;
        ad(x,y,z);
        if(!t)ad(y,x,z);
    }
    int num = 1e15 + 7;
    for(int i = 1;i <= n;++i)
        for(int j = hd[i];j;j = nx[j]){
            int y = to[j],vv = vx[j];
            vx[j] = 1e15 + 7;
            if(!t)vx[j ^ 1] = 1e15 + 7;
            num = min(num,vv + dj(y,i));
            vx[j] = vv;
            if(!t)vx[j ^ 1] = vv;
        }
    if(num == 1e15 + 7){
        cout<<"-1";
        return 0;
    }
    cout<<num;
    return 0;
}
0