free 【spfa+dp】

题目描述 

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.

示例1

输入

复制

3 2 1 3 1
1 2 1
2 3 2

输出

复制

1

备注:

1 \le n,m \le 10^3,1 \le S,T,a,b \le n,0 \le k \le m,1 \le l \le 10^61≤n,m≤103,1≤S,T,a,b≤n,0≤k≤m,1≤l≤106.
Multiple edges and self loops are allowed.

题目大意:n个点,m条边,可以任意去掉k条边,问从s到t的最短路径

解题思路:用spfa求最短路径。dp[i][j]表示从起始点s到点i去掉j条边的最短路径,dp方程为dp[i][j]=min(dp[i][j],min(dp[i-1][j]+val,dp[i-1][j-1])。

AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define io ios::sync_with_stdio(0),cin.tie(0)
#define ms(arr) memset(arr,0,sizeof(arr))
#define mc(a,b) memcpy(a,b,sizeof(b))
#define inf 0x3f3f3f
#define fin freopen("in.txt", "r", stdin)
#define fout freopen("out.txt", "w", stdout)
typedef long long ll;
typedef unsigned long long ULL;
const int mod=1e9+7;
const int N=1e5+7;

struct node
{
    int to,val;
};

int dp[1100][1100];
int inq[1100];
int n,m,s,t,k,x,y,z;
vector <node> g[1100];
void add_node(int u,int v,int val)///建图
{
    g[u].push_back(node{v,val});
    g[v].push_back(node{u,val});
}
void spfa()///spfa求最短路径
{
    int t;
    queue <int> q;
    q.push(s);
    inq[s]=1;
    while(!q.empty())
    {
        t=q.front();
        inq[t]=0;
        q.pop();
        for(int i=0;i<g[t].size();i++){
            node c=g[t][i];
            int temp=dp[c.to][0];
            dp[c.to][0]=min(dp[c.to][0],dp[t][0]+c.val);
            if(dp[c.to][0]!=temp&&inq[c.to]==0){
                q.push(c.to);
                inq[c.to]=1;
            }
            ///dp部分代码
            for(int j=1;j<=k;j++){
                int temp=dp[c.to][j];
                dp[c.to][j]=min(dp[c.to][j],min(dp[t][j]+c.val,dp[t][j-1]));
                if(dp[c.to][j]!=temp&&inq[c.to]==0){
                    q.push(c.to);
                    inq[c.to]=1;
                }
            }
        }
    }
}
int main()
{
//    fin;
    scanf("%d%d%d%d%d",&n,&m,&s,&t,&k);
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&x,&y,&z);
        add_node(x,y,z);
        add_node(y,x,z);
    }
    ///初始化dp数组
    memset(dp,inf,sizeof(dp));
    for(int i=0;i<=k;i++) dp[s][i]=0;
    spfa();
    printf("%d\n",dp[t][k]);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值