結果

問題 No.845 最長の切符
ユーザー 増井舜増井舜
提出日時 2020-07-13 00:22:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 468 ms / 3,000 ms
コード長 3,330 bytes
コンパイル時間 1,906 ms
コンパイル使用メモリ 176,396 KB
実行使用メモリ 18,284 KB
最終ジャッジ日時 2024-04-24 02:14:57
合計ジャッジ時間 5,818 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 6 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 69 ms
5,860 KB
testcase_16 AC 468 ms
18,284 KB
testcase_17 AC 71 ms
5,860 KB
testcase_18 AC 166 ms
8,680 KB
testcase_19 AC 29 ms
5,376 KB
testcase_20 AC 448 ms
18,148 KB
testcase_21 AC 404 ms
18,280 KB
testcase_22 AC 70 ms
5,736 KB
testcase_23 AC 14 ms
5,376 KB
testcase_24 AC 460 ms
18,156 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 181 ms
12,132 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 356 ms
14,312 KB
testcase_29 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#include<vector>
#include<iostream>
#include<queue>
#include<algorithm>
#include<map>
#include<set>
#include<iomanip>
#include<assert.h>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<stack>
#include<complex>
#include<memory>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse2")

#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;
using Graph=vector<vector<int>>;
using ll=long long;
	
#define WHATS(var)cout<<__LINE__<<' '<<#var<<"="<<var<<endl;
	
template<class S, class T> ostream& operator <<(ostream &os, const pair<S, T> v){
	os << "( " << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<T> &v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<vector<T>> &v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<set<T>> &v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const set<T> &v){
	int i=0;
	for(auto it:v){
		if(i > 0){os << ' ';}
		os << it;
		i++;
	} 
	return os;
}
//1. maskの部分集合を列挙する
	// 	a. i==0を評価しない
	// 	for(int i=mask; i>0; i=(i-1)&mask) {
	// 	}

	// b. i==0を評価する
	// 	for(int i=mask; i>=0; i--) {
	// 		i&=mask;
	// 	}
// 2. maskを含む集合を列挙する

// 少し目的は変わりますが同じようなやり方でできる処理が、maskを含む集合の列挙です。

// 	for(int i=mask; i<(1<<n); i=(i+1)|mask) {
// 	}

	
using ll = long long int;
int main() {
	ios::sync_with_stdio(false);
	cin.tie();
	int N,M;cin>>N>>M;
    vector<vector<int>>costs(N,vector<int>(N,1e7));
    for(int i=0;i<M;++i){
        int a,b,c;cin>>a>>b>>c;
        a--;b--;
        c=int(1e5)-c;
        costs[a][b]=min(costs[a][b],c);
        costs[b][a]=min(costs[b][a],c);
    }
    //WHATS(costs)
    vector<vector<int>>memo(1<<N,vector<int>(N,int(1e9)));
    priority_queue<pair<pair<int,int>,pair<int,int>>>que;
    for(int i=0;i<N;++i){
        memo[1<<i][i]=0;
        que.emplace(make_pair(-1,0),make_pair(1<<i,i));
    }
    while(!que.empty()){
        auto p(que.top());
        que.pop();
        int cnt=-p.first.first;
        int cost=-p.first.second;
        int nb=p.second.first;
        int now=p.second.second;
        // WHATS(nb)
        // WHATS(now)
        // WHATS(cnt)
        // WHATS(cost)
        for(int ne=0;ne<N;++ne){
            if(!(nb&(1<<ne))){
                int nextb=nb|(1<<ne);
                int nextcost=cost+costs[now][ne];
                if(memo[nextb][ne]>nextcost){
                    memo[nextb][ne]=nextcost;
                    que.push(make_pair(make_pair(-cnt-1,-nextcost),make_pair(nextb,ne)));
                }
            }
        }
    }
    int answer=-1;
    for(int i=0;i<(1<<N);++i){
        for(int j=0;j<N;++j){
            int cost=memo[i][j];
            int cnt=__builtin_popcount(i);
            answer=max(answer,(cnt-1)*int(1e5)-cost);
        }
    }
    cout<<answer<<endl;
	
	return 0;
}
0