zoukankan      html  css  js  c++  java
  • CF1005F Berland and the Shortest Paths

    (color{#0066ff}{ 题目描述 })

    一个无向图(边权为1),输出一下选边的方案使(sum d_i)最小((d_i)为从1到i的最短路)

    输出一个方案数和方案(方案数超过k个只需输出k个)

    (color{#0066ff}{输入格式})

    第一行n,m,k,为点数,边数和k

    接下来m行为无向边(权为1)

    (color{#0066ff}{输出格式})

    第一行为方案数(超过k个只输出k个)

    接下来是01表示的方案(每条边选或不选)

    (color{#0066ff}{输入样例})

    4 4 3
    1 2
    2 3
    1 4
    4 3
    
    
        
    4 6 3
    1 2
    2 3
    1 4
    4 3
    2 4
    1 3
    
    
        
        
    5 6 2
    1 2
    1 3
    2 4
    2 5
    3 4
    3 5
    

    (color{#0066ff}{输出样例})

    2
    1110
    1011
    
    
    
    
    1
    101001
    
    
        
        
    2
    111100
    110110
    

    (color{#0066ff}{数据范围与提示})

    (nleq 2*10^5, n-1leq m leq 2*10^5, 1 leq k leq 2*10^5, m*kleq 10^6)

    (color{#0066ff}{ 题解 })

    显然这题要求的是最短路图的生成树方案

    先找出最短路图,根据dis(到1距离)分层

    然后从2开始每个点都有向上连边的方案,统计一下

    (O(m*k))输出方案(搜索,枚举)

    #include<bits/stdc++.h>
    #define LL long long
    LL in() {
    	char ch; LL x = 0, f = 1;
    	while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
    	for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
    	return x * f;
    }
    LL n, m, k;
    const int maxn = 2e5 + 100;
    struct node {
    	int to, id;
    	node *nxt;
    	node(int to = 0, int id = 0, node *nxt = NULL): to(to), id(id), nxt(nxt) {}
    	void *operator new(size_t) {
    		static node *S = NULL, *T = NULL;
    		return (S == T) && (T = (S = new node[1024]) + 1024), S++;
    	}
    };
    using std::pair;
    using std::make_pair;
    std::priority_queue<pair<int, int>, std::vector<pair<int, int> >, std::greater<pair<int, int> > > q;
    std::queue<int> v;
    int cnt[maxn], dis[maxn], du[maxn];
    bool vis[maxn];
    node *head[maxn], *h[maxn];
    void add(int from, int to, int id, node **hh) {
    	hh[from] = new node(to, id, hh[from]);
    }
    void dij() {
    	for(int i = 1; i <= n; i++) dis[i] = maxn;
    	q.push(make_pair(dis[1] = 0, 1));
    	while(!q.empty()) {
    		int tp = q.top().second; q.pop();
    		if(vis[tp]) continue;
    		vis[tp] = true;
    		for(node *i = head[tp]; i; i = i->nxt)
    			if(dis[i->to] > dis[tp] + 1)
    				q.push(make_pair(dis[i->to] = dis[tp] + 1, i->to));
    	}
    }
    void toposort() {
    	LL tot = 1;
    	for(int i = 2; i <= n; i++) {
    		tot *= du[i];
    		if(tot >= k) break;
    	}
    	k = std::min(tot, k);
    	printf("%lld
    ", k);
    }
    void dfs(int d) {
    	if(!k) return;
    	if(d == n + 1) {
    		for(int i = 1; i <= m; i++) printf("%d", vis[i]);
    		puts("");
    		k--;
    		return;
    	}
    	for(node *i = h[d]; i; i = i->nxt) {
    		vis[i->id] = true;
    		dfs(d + 1);
    		vis[i->id] = false;
    		if(!k) return;
    	}
    }
    int main() {
    	n = in(), m = in(), k = in();
    	int x, y;
    	for(int i = 1; i <= m; i++) {
    		x = in(), y = in();
    		add(x, y, i, head);
    		add(y, x, i, head);
    	}
    	dij();
    	for(int i = 1; i <= n; i++)
    		for(node *j = head[i]; j; j = j->nxt)
    			if(dis[j->to] == dis[i] + 1)
    				add(j->to, i, j->id, h), du[j->to]++;
    	toposort();
    	for(int i = 1; i <= m; i++) vis[i] = 0;
    	dfs(2);
    	return 0;
    }
    
  • 相关阅读:
    【安全测试】sqlmap安装方法
    【安全测试】 WebScarab安装方法
    【安全测试】burpsuite安装方法
    【Jenkins】Windows下安装&访问jenkins
    【Python】下拉框元素的找法
    【python】selenium+python自动化测试环境搭建
    【接口测试】Jenkins+Ant+Jmeter搭建持续集成的接口测试平台
    【修改端口号】linux下修改apache,nginx服务端口号
    【linux】linux下yum安装后Apache、php、mysql默认安装路径
    地址
  • 原文地址:https://www.cnblogs.com/olinr/p/10266412.html
Copyright © 2011-2022 走看看