迪杰斯特拉加DP

题目链接:https://vjudge.net/problem/Gym-101635E

题目大意:

给定一个有 N 条边的有向无环图(有多个起点),每条边都有其费用和收益,现要从一个或多个起点出发,以某一个或多个点为终点(一个点不能多次作为终点;如果有多个方案能到达同一个点,则选择总费用最少的),问在使得总费用不超过 B 的大前提下,能得到的最大收益(如果有多个得到最大收益的方法,则选择使得费用最少的)。输出最大收益及其对应的总费用。

(建议仔细地读一下 ImportantNotes)

知识点:  DP、最短路

解题思路:

先用 Dijkstra 跑出从各个起点到各个点的最小费用及其对应的收益(如果有多个费用最小的方案,则选择收益最大的那个),再用 dp 计算出在各个总费用下所能得到的最大收益。

AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxm = 1000000+5, maxn = 10000+5, inf=0x3f3f3f3f;

struct Edge{
int to,nxt,cost,prestige;
}edge[maxm];
int head[maxm],tot;
void init(){
memset(head,-1,sizeof(head));
tot=0;
}
void addedge(int u,int v,int cost,int pres){
edge[tot].to=v;
edge[tot].nxt=head[u];
edge[tot].prestige=pres;
edge[tot].cost=cost;
head[u]=tot++;
}

int min_cost[maxn],max_pres[maxn];
bool noroot[maxn];
map<string,int> ind;

void dijkstra(int s){
queue que;
que.push(s);
min_cost[s]=max_pres[s]=0;
while(!que.empty()){
int v=que.front();
que.pop();
for(int i=head[v];i!=-1;i=edge[i].nxt){
int to=edge[i].to;
if(min_cost[to]>min_cost[v]+edge[i].cost){
min_cost[to]=min_cost[v]+edge[i].cost;
max_pres[to]=max_pres[v]+edge[i].prestige;
que.push(to);
}
else if(min_cost[to]==min_cost[v]+edge[i].cost&&max_pres[to]<max_pres[v]+edge[i].prestige){
max_pres[to]=max_pres[v]+edge[i].prestige;
que.push(to);
}
}
}
}
int dp[maxn];
int main(){
std::ios::sync_with_stdio(false); //如果没有加速输入输出的话会T
int B,N,cos,pre,u,v;
int cnt=1;
string to,from,tmp;
cin>>B>>N;
init();
for(int i=0;i<N;i++){
cin>>to>>from>>tmp>>cos>>pre;
if(!ind[to])
ind[to]=cnt++;
v=ind[to];
if(!ind[from])
ind[from]=cnt++;
u=ind[from];
addedge(u,v,cos,pre);
noroot[v]=true;
}
for(int i=1;i<cnt;i++)
min_cost[i]=inf,max_pres[i]=0;
for(int i=1;i<cnt;i++){
if(!noroot[i])
dijkstra(i);
}
dp[0]=0;
for(int i=1;i<cnt;i++){
for(int j=B;j>=min_cost[i];j–)
dp[j]=max(dp[j],dp[j-min_cost[i]]+max_pres[i]);
}
int ans1=0,ans2=0;
for(int i=0;i<=B;i++){
if(dp[i]>ans1)
ans1=dp[i],ans2=i;
}
cout<<ans1<<endl;
cout<<ans2<<endl;

return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值