涨姿势的一道题,最短路与二分的结合,求得在限定距离内,获得价值的最大值
题解:二分答案(获得的最多财宝价值),Dijkstra求最短路,在获得价值为mid的情况下,如果能在时间k内走到终点,则返回true,否则返回false
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<string.h>
using namespace std;
const int maxn = 1e4 + 5;
const int inf = 2e9 + 1;
typedef pair<int,int> PII;
struct Edge{
int v,cap,dis,nxt;
}edge[maxn*10];
int head[maxn],tot,d[maxn],k,n;
void add(int u,int v,int cap,int dis){
edge[tot].v=v;
edge[tot].cap=cap;
edge[tot].dis=dis;
edge[tot].nxt=head[u];
head[u]=tot++;
}
bool ok(int mid){
for(int i=1;i<=n;i++) d[i]=inf;
d[1]=0;
priority_queue<PII,vector<PII>,greater<PII> >q;
q.push(PII(0,1));
while(!q.empty()){
PII p=q.top();q.pop();
int u=p.second;
if(d[u]<p.first) continue;
for(int i=head[u];~i;i=edge[i].nxt){
int v=edge[i].v;
if(d[u]+edge[i].dis<=k&&d[v]>d[u]+edge[i].dis&&edge[i].cap>=mid){
d[v]=d[u]+edge[i].dis;
q.push(PII(d[v],v));
}
}
}
if(d[n]>k) return false;
return true;
}
int main(){
int m,T;
// freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--){
memset(head,-1,sizeof(head));
tot=0;
scanf("%d%d%d",&n,&m,&k);
int l=0,r=0;
for(int i=0;i<m;i++){
int u,v,dis,cap;
scanf("%d%d%d%d",&u,&v,&cap,&dis);
add(u,v,cap,dis);
add(v,u,cap,dis);
r=max(r,cap);
}
int ans=-1;
while(l<=r){
int mid=(l+r)>>1;
if(ok(mid)){
l=mid+1;
ans=mid;
}
else r=mid-1;
}
if(ans!=-1) printf("%d\n",r);
else printf("Poor RunningPhoton!\n");
}
return 0;
}