zoukankan      html  css  js  c++  java
  • CF1076D 最短路树

    题意:n个点m条边,最多可以删除m-k条边,使得剩下的边为构成原图的最短路树。
    思路:dij之后跑一边dfs最短路树。
    代码:

    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define forn(i,n) for(int i=0;i<n;i++)
    #define for1(i,n) for(int i=1;i<=n;i++)
    #define IO ios::sync_with_stdio(false);cin.tie(0)
    const int maxn = 3e5+5;
    const ll inf = 2e18+5;
    #pragma GCC optimize(2)
    int n,m,k;
    struct node{
        int w,id,p;
    }nd[maxn];
    ll d[maxn];
    bool vis[maxn];
    vector<node>e[maxn];
    
    void dfs(int u){
        vis[u] = 1;
        for(auto &x:e[u]){
            int v = x.p,id = x.id,w = x.w;
            if(!vis[v]&&d[v]==d[u]+w){
                if(!k) return;
                cout<<id<<' ';
                k--;
                dfs(v);
            }
        }    
    }
    
    int main(){
        IO;
        forn(i,maxn) d[i] = inf;
        cin>>n>>m>>k;
        for1(i,m){
            int x,y,z;cin>>x>>y>>z;
            e[x].push_back({z,i,y});
            e[y].push_back({z,i,x});
        }
        priority_queue<pair<ll,int> > q;
        q.push({0,1});
        d[1] = 0;
        while(!q.empty()){
            auto now = q.top();q.pop();
            int u = now.second;
            if(vis[u]) continue;
            vis[u] = 1;
            for(auto &x:e[u])if(!vis[x.p]){
                int v = x.p,w = x.w;
                if(d[v]>d[u]+w){
                    d[v]=d[u]+w;
                    q.push({-d[v],v});
                }
            }
        }
        forn(i,maxn) vis[i] = 0;
        cout<<min(n-1,k)<<'
    ';
        vector<int>ans;
        dfs(1);
        return 0;
    }
    
  • 相关阅读:
    indexedDB 增删改查
    css-包含块
    css解析规则
    css样式---隐藏元素
    javascript实现数据结构----栈
    Android Studio 显示 logcat
    Android Spinner 绑定键值对
    Java json转model
    this view is not constrainted......
    Android 访问 Webapi 更新UI
  • 原文地址:https://www.cnblogs.com/AlexPanda/p/12520294.html
Copyright © 2011-2022 走看看