题目链接:http://acm.scu.edu.cn/soj/problem.action?id=4527
中文题,就无需讲题意了。
二分大法好。。。直接枚举答案。
每次二分都是得最小可以携带的财宝。
AC代码:
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn = 1e4 + 10;
const int inf = 0x3f3f3f3f;
int head[maxn];
struct node{
int nex;
int u,v,cap,w;
node(){}
node(int u,int v,int cap,int w,int nex):
u(u),v(v),cap(cap),w(w),nex(nex){}
}mp[maxn*10];
int dis[maxn];
struct pi{
int v,dis;
pi(){}
pi(int v,int dis):v(v),dis(dis){}
friend bool operator < (const pi &A,const pi &B){
return A.dis > B.dis;
}
};
bool dijk(int t,int k,int low){
memset(dis,inf,sizeof(dis));
dis[1] = 0;
priority_queue<pi>Q;
Q.push(pi(1,0));
while(!Q.empty()){
int now = Q.top().v;
Q.pop();
for(int i = head[now]; ~i ;i = mp[i].nex){
int v = mp[i].v;
if(dis[v] > dis[now] + mp[i].w && mp[i].cap >= low){
dis[v] = dis[now] + mp[i].w;
Q.push(pi(v,mp[i].w));
}
}
}
if(dis[t] > k) return 0;
return 1;
}
int main()
{
int n,m,k;
int t; scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&k);
memset(head,-1,sizeof(head));
int a,b,c,d; int cnt = 0;
for(int i = 0;i < m;i ++){
scanf("%d%d%d%d",&a,&b,&c,&d);
mp[cnt] = node(a,b,c,d,head[a]);
head[a] = cnt ++;
mp[cnt] = node(b,a,c,d,head[b]);
head[b] = cnt ++;
}
int l = 0,r = 0x3f3f3f3f;
int ans = 0;
int flag = 0;
while(l <= r){
int mid = (l + r) >> 1;
if(dijk(n,k,mid)){ l = mid + 1; flag = 1; }
else r = mid - 1;
}
if(flag) printf("%d\n",r);
else printf("Poor RunningPhoton!\n");
}
return 0;
}