zoukankan      html  css  js  c++  java
  • free

    问题 J: free

    时间限制: 2 Sec  内存限制: 128 MB
    提交: 4  解决: 3
    [提交] [状态] [命题人:admin]

    题目描述

    Your are given an undirect connected graph.Every edge has a cost to pass.You should choose a path from S to T and you need to pay for all the edges in your path. However, you can choose at most k edges in the graph and change their costs to zero in the beginning. Please answer the minimal total cost you need to pay.

    输入

    The first line contains five integers n,m,S,T,K.
    For each of the following m lines, there are three integers a,b,l, meaning there is an edge that costs l between a and b.
    n is the number of nodes and m is the number of edges.

    输出

    An integer meaning the minimal total cost.

    样例输入

    3 2 1 3 1
    1 2 1
    2 3 2
    

    样例输出

    1
    

    提示

    1≤n,m≤103,1≤S,T,a,b≤n,0≤k≤m,1≤l≤106.
    Multiple edges and self loops are allowed.

    #include<bits/stdc++.h>
    #include<iostream>
    
    #define ll long long
    using namespace std;
    const int p = 1e9 + 7;
    const int maxn = 2e3 + 10;
    
    struct path{
        int to,nx,w;
    }o[maxn];
    
    struct atom{
        int ver,val,cost;
        atom(int _ver,int _val,int _cost):ver(_ver),val(_val),cost(_cost){}
        bool operator<(const atom& cur)const {
            return val>cur.val;
        }
    };
    
    int n,m,S,T,k,tot,head[maxn],vis[maxn][maxn];
    ll dp[maxn][maxn];
    
    inline void add_edge(int u,int v,int w){
        o[++tot].to=v;
        o[tot].w=w;
        o[tot].nx=head[u];
        head[u]=tot;
    }
    
    inline void dij(){
        for(register int i=1;i<=n;++i){
            for(register int j=0;j<=k;++j){
                dp[i][j]=-1;
            }
        }
        priority_queue<atom>q;
        q.push(atom(S,0,k));
        dp[S][k]=0;
        while(!q.empty()){
            atom cur=q.top();
            q.pop();
            int point=cur.ver,cnt=cur.cost;
            if(vis[point][cnt])continue;
            vis[point][cnt]=1;
            for(register int i=head[point];i;i=o[i].nx){
                int to=o[i].to;
                if(cnt>0&&(dp[to][cnt-1]==-1||dp[point][cnt]<dp[to][cnt-1])){
                    dp[to][cnt-1]=dp[point][cnt];
                    q.push(atom(to,dp[to][cnt-1],cnt-1));
                }
                if(dp[to][cnt]==-1||dp[to][cnt]>dp[point][cnt]+o[i].w){
                    dp[to][cnt]=dp[point][cnt]+o[i].w;
                    q.push(atom(to,dp[to][cnt],cnt));
                }
            }
        }
    }
    
    
    int main() {
    #ifndef ONLINE_JUDGE
        freopen("1.txt","r",stdin);
    #endif
        scanf("%d%d%d%d%d",&n,&m,&S,&T,&k);
        for(register int i=1;i<=m;++i){
            int a,b,l;
            scanf("%d%d%d",&a,&b,&l);
            add_edge(a,b,l);
            add_edge(b,a,l);
        }
        dij();
        ll res=INT_MAX/2;
        for(register int i=0;i<=k;++i){
            if(dp[T][i]!=-1)res=min(res,dp[T][i]);
        }
        printf("%lld
    ",res);
        return 0;
    }
  • 相关阅读:
    JS图片不间断滚动代码(向上,向下,向左,向右)
    存储过程int型转字符型
    DataList编辑、更新、取消、删除、分页
    js设置焦点
    iFrame只要竖滚动条,不要横滚动条
    C#中通过值和引用传递参数
    关于C#值类型,引用类型,值传递,引用传递
    提醒自我
    c#读取html文件内容替换之后再写入
    sql中替换字符串
  • 原文地址:https://www.cnblogs.com/czy-power/p/11462191.html
Copyright © 2011-2022 走看看