首页 技术 正文
技术 2022年11月20日
0 收藏 726 点赞 3,886 浏览 1458 个字

原题:http://www.luogu.org/problem/show?pid=1462#sub

4boy:

大意:给出n个城市,有m条路,每经过一个城市都要交钱,每经过一条道路都要扣HP,有HP上限;要从1走到n,

求在HP没扣完的情况下,从1到n经过城市的最大花费的最小值。

思路:用二分搜索cost的值的最小值,用spfa搜索从1到n所需的最少HP,在spfa中判断cost[i]与x详见代码注释;

 #include<iostream>
#include<cstring>
#include<queue>
#define maxn 100000
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
queue<int> q;
int n,m;
int edgenum=,link[maxn],v[maxn];
ll b,ans=-,max_c=-,cost[maxn],dis[maxn];
struct EDGE //边表优化
{
int v,next;
ll val;
EDGE(){next=;}
}edge[maxn];
void add(int a,int b,int x)
{
edgenum++;
edge[edgenum].v=b;
edge[edgenum].val=x;
edge[edgenum].next=link[a];
link[a]=edgenum;
}
void init()
{
cin>>n>>m>>b;
for(int i=;i<=n;i++)
{
cin>>cost[i];
max_c=max(max_c,cost[i]);
}
int x,y,l;
for(int i=;i<=m;i++)
{
cin>>x>>y>>l;
add(x,y,l);
add(y,x,l);
}
}
bool spfa(ll x)
{
memset(dis,INF,sizeof(dis));
memset(v,,sizeof(v));
dis[]=;
v[]=;
q.push();
while(!q.empty())
{
int temp=q.front();
q.pop();
v[temp]=;
for(int i=link[temp];i!=;i=edge[i].next)
{
if(cost[edge[i].v]>x) continue; //此城市的cost>期望最小值x,则跳过
if(dis[edge[i].v]>dis[temp]+edge[i].val)
{
dis[edge[i].v]=dis[temp]+edge[i].val;
if(!v[edge[i].v])
{
q.push(edge[i].v);
v[edge[i].v]=;
}
}
}
}
if(dis[n]>b || dis[n]==(ll)INF) return false; //最短路的HP>上限 || 不通 则false
return true;
}
void work()
{
ll mid;
ll l=,r=max_c;
while(l<=r)
{
mid=(r+l)>>;
if(spfa(mid)==true) //已mid为值的cost可通,记录答案并搜索区间左移
{
r=mid-;
ans=mid;
}
else //不通,搜索区间右移
{
l=mid+;
}
}
if(ans==-) cout<<"AFK";
else cout<<ans;
}
void pt()
{
//if(spfa(10)) cout<<"fuck you";
}
int main()
{
init();
work();
//pt();
//system("pause");
return ;
}

还有,请叫我子程序狂魔=-=

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,960
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,484
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,330
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,113
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,745
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,779