zoukankan      html  css  js  c++  java
  • D. Fair(Codeforces Round #485 (Div. 2))

    D. Fair
    time limit per test
    2 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    Some company is going to hold a fair in Byteland. There are nn towns in Byteland and mm two-way roads between towns. Of course, you can reach any town from any other town using roads.

    There are kk types of goods produced in Byteland and every town produces only one type. To hold a fair you have to bring at least ssdifferent types of goods. It costs d(u,v)d(u,v) coins to bring goods from town uu to town vv where d(u,v)d(u,v) is the length of the shortest path from uuto vv. Length of a path is the number of roads in this path.

    The organizers will cover all travel expenses but they can choose the towns to bring goods from. Now they want to calculate minimum expenses to hold a fair in each of nn towns.

    Input

    There are 44 integers nnmmkkss in the first line of input (1n1051≤n≤1050m1050≤m≤1051skmin(n,100)1≤s≤k≤min(n,100)) — the number of towns, the number of roads, the number of different types of goods, the number of different types of goods necessary to hold a fair.

    In the next line there are nn integers a1,a2,,ana1,a2,…,an (1aik1≤ai≤k), where aiai is the type of goods produced in the ii-th town. It is guaranteed that all integers between 11 and kk occur at least once among integers aiai.

    In the next mm lines roads are described. Each road is described by two integers uu vv (1u,vn1≤u,v≤nuvu≠v) — the towns connected by this road. It is guaranteed that there is no more than one road between every two towns. It is guaranteed that you can go from any town to any other town via roads.

    Output

    Print nn numbers, the ii-th of them is the minimum number of coins you need to spend on travel expenses to hold a fair in town ii. Separate numbers with spaces.

    Examples
    input
    Copy
    5 5 4 3
    1 2 4 3 2
    1 2
    2 3
    3 4
    4 1
    4 5
    
    output
    Copy
    2 2 2 2 3 
    
    input
    Copy
    7 6 3 2
    1 2 3 3 2 2 1
    1 2
    2 3
    3 4
    2 5
    5 6
    6 7
    
    output
    Copy
    1 1 1 2 2 1 1 

    思路:多源最短路

    AC:代码

    时间:960ms

    #include <bits/stdc++.h>
    
    
    using namespace std;
    
    const int maxn = 1e5+5;
    #define INF  0x3f3f3f3f
    
    int good[maxn],path[maxn][105],vis[maxn];
    int n,m,s,k;
    
    vector<int>G[maxn];
    vector<int>E[120];
    
    struct node{
    	int x,s;
    	node(int _x,int _s)
    	{
    		x = _x;
    		s = _s;
    	}
    };
    
    void bfs(int st)
    {
    	queue<node>que;
    	for(int i = 0;i<E[st].size();i++){
    		int e = E[st][i];
    		que.push(node(e,0));
    		path[E[st][i]][st] = 0;
    		vis[E[st][i]] = 1;
    	}
    	while(!que.empty())
    	{
    		node q = que.front();que.pop();
    		path[q.x][st] = min(q.s,path[q.x][st]);
    		for(int i = 0;i<G[q.x].size();i++){
    			int e = G[q.x][i];
    			if(vis[e]) continue;
    			vis[e] = 1;
    			que.push(node(e,q.s+1));
    		}
    	}
    }
    
    int main()
    {
    	int a,b;
    	scanf("%d %d %d %d",&n,&m,&s,&k);
    	for (int i = 1;i<=n;i++){
    		scanf("%d",&good[i]);
    		E[good[i]].push_back(i);
    	}
    	for (int i = 1;i<=m;i++){
    		scanf("%d %d",&a,&b);
    		G[a].push_back(b);
    		G[b].push_back(a);
    	}
    	memset(path,INF,sizeof(path));
    	for(int i = 1;i<=s;i++){
    		memset(vis,0,sizeof(vis));
    		bfs(i);
    	}
    	long long sum;
    	for (int i = 1;i<=n;i++){
    		sum = 0;
    		sort(path[i]+1,path[i]+s+1);
    		for(int j = 1;j<=k;j++){
    			sum += path[i][j];
    		}
    		printf("%lld ",sum);
    	}
    	return 0;
    }


  • 相关阅读:
    Linux 4.11 内核变化
    c++设计模式
    【MySQL】undo,redo,2PC,恢复思维导图
    10053
    深入理解MySQL中的Redo、Undo、MVCC
    oracle 博客精选
    Linux内存中的Cache真的能被回收么?
    MySQL性能指标及计算方法 等待show processlist
    HTTP抓包工具Fiddler
    科来网络分析
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11746000.html
Copyright © 2011-2022 走看看