蒟蒻40pts求调(悬关)
查看原帖
蒟蒻40pts求调(悬关)
767155
a_small_penguin楼主2024/10/24 12:59

code:

#include <bits/stdc++.h>

using namespace std;

typedef pair<pair<int, int> , vector<int>> PII;
typedef pair<pair<int, int>, int> PIII;

constexpr int N = 3009;

int n, m,p,k;
vector<PII> g[N];
int dist[N][1<<13];
bool st[N][1<<13];
vector<int> a[N]; 

void dijkstra(int Start) {
    memset(dist, 0x3f, sizeof(dist));
    memset(st, false, sizeof(st));
    bitset<19> q = 0;
	priority_queue<PIII, vector<PIII>, greater<PIII>> heap;
	for(auto it:a[1]){
		q[it] = 1;
	}
    heap.push({{0,Start},q.to_ullong()});
	dist[1][q.to_ullong()] = 0;
    while (!heap.empty()) {
        auto t = heap.top();
        heap.pop();
        int ver = t.first.second, dissss = t.first.first;
        q = t.second;
       	for(auto it:a[ver]) q[it] = 1;
        if (st[ver][q.to_ullong()]) continue;
        st[ver][q.to_ullong()] = true;
        for (const auto &it : g[ver]) {
            int to = it.first.first, val = it.first.second;
            vector<int> ccc = it.second;
            bool c = 1;
            for(auto ti : ccc){
            	c  &= q[ti];
			}
			if(!c) continue;
            if (dist[to][q.to_ullong()] > dist[ver][q.to_ullong()] + val) {
                dist[to][q.to_ullong()] = dist[ver][q.to_ullong()] + val;
                heap.push({{dist[to][q.to_ullong()], to},q.to_ullong()});
            }
        }
    }
}

int main() {
    cin >> n >> m >> p >> k;
    for(int i = 1;i<=k;i++){
    	int x,zzz;
    	cin >> x >> zzz;
    	for(int j = 1;j<=zzz;j++){
    		int y;
    		cin >> y;
    		a[x].push_back(y);
		}
	}
    for (int i = 0; i < m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        vector<int> ccc;
        int c;
        cin >> c;
        for(int j = 0;j<c;j++){
        	int x;
        	cin >> x;
        	ccc.push_back(x);
		}
        g[u].push_back({{v,w},ccc});
        g[v].push_back({{u,w},ccc});
    }

    dijkstra(1);
    int minn = 0x3f3f3f3f;
    for(int i = 0;i<=(1<<13)-1;i++){
    	minn = min(minn,dist[n][i]);
	}
	if (minn == 0x3f3f3f3f) {
        cout << "-1\n";
        return 0;
    }
    cout << minn << "\n";
    return 0;
}
2024/10/24 12:59
加载中...