ccf 201903-5 317号子任务【60分】

#include
using namespace std;
struct node{
     
    int u;
    int x;
};
vector<node> g[10005];
int n,m,k;
vector<int> ju_dian;
long long dis1[10005];
vector<long long> dis2[10005];
bool visited[10005];
const long long INF = 1e15;
int num[10005];
void spfa(int s){
     
    for(int i = 0;i < n;i++){
     
        visited[i] = 0;
        dis1[i] = INF;
        num[i] = 0;
    }
    queue<int> Q;
    visited[s] = 1;
    num[s]++;
    dis1[s] = 0;
    Q.push(s);
    while(!Q.empty()){
     
        int top = Q.front();
        Q.pop();
        visited[top] = 0;
        for(int i = 0;i < g[top].size();i++){
     
            int id = g[top][i].u;
            int x = g[top][i].x;
            if(dis1[id] > dis1[top] + x){
     
                dis1[id] = dis1[top] + x;
                if(!visited[id]){
     
                    visited[id] = 1;
                    num[id]++;
                    //if(num[id] >= n) return;
                    Q.push(id);
                }
            }
        }
    }
    for(int i = 0;i < n;i++){
     
        if(dis1[i] != INF){
     
            dis2[i].push_back(dis1[i]);
        }

    }
}

int main(){
     
    ios::sync_with_stdio(0);
    cin >> n >> m >> k;
    for(int i = 0;i < n;i++){
     
        int t;cin >> t;
        if(t == 1) ju_dian.push_back(i);
    }
    for(int i = 0;i < m;i++){
     
        int a,b,c; cin >> a >> b >> c;
        a--;b--;
        node temp;
        temp.u = b;
        temp.x = c;
        g[a].push_back(temp);
        temp.u = a;
        g[b].push_back(temp);
    }
    for(int i = 0;i < ju_dian.size();i++){
     
        spfa(ju_dian[i]);
    }
    for(int i = 0;i < n;i++){
     
        long long sum = 0;
        sort(dis2[i].begin(),dis2[i].end());
        for(int j = 0;j < dis2[i].size() && j < k;j++){
     
            sum += dis2[i][j];
        }
        cout << sum << endl;
    }


}

你可能感兴趣的:(ccf试题)