丛林探险

vijos P1082
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <iostream>
#include "algorithm"
using namespace std;
typedef long long LL;
const int MAX1=5005;
const int MAX2=80005;
int n,m;
int tot;
int head[MAX1],adj[MAX2],next[MAX2],len[MAX2],str[MAX2];
int dist[MAX1];
int cost[MAX1];
int s,t,k;
void addedge(int u,int v,int c,int d){
    tot++;
    adj[tot]=v;
    next[tot]=head[u];
    str[tot]=c;
    len[tot]=d;
    head[u]=tot;
}
void init(){
    int i,j;
    int u,v,c,d;
    scanf("%d%d",&n,&m);
    memset(head,0,sizeof(head));
    memset(next,0,sizeof(next));
    for (i=1;i<=m;i++)
    {scanf("%d%d%d%d",&u,&v,&c,&d);
     addedge(u,v,c,d);
     addedge(v,u,c,d);
    }
    scanf("%d%d%d",&s,&t,&k);
}
bool SPFA(int mid){
    int i,j;
    queue <int> q;
    bool flag[MAX1];
    memset(dist,0x3f,sizeof(dist));
    memset(cost,0,sizeof(cost));
    memset(flag,false,sizeof(flag));
    q.push(s);
    flag[s]=true;
    dist[s]=0;
    while (!q.empty())
    {int u;
     u=q.front();
     q.pop();
     flag[u]=true;
     for (i=head[u];i;i=next[i])
      if (cost[u]+str[i]<=k && dist[u]+len[i]<dist[adj[i]])
      {cost[adj[i]]=cost[u]+str[i];
       dist[adj[i]]=dist[u]+len[i];
       if (flag[adj[i]]==false)
       {flag[adj[i]]=true;
        q.push(adj[i]);
       }
      }
    }
    return (dist[t]<=mid);
}
int main(){
    freopen ("explor.in","r",stdin);
    freopen ("explor.out","w",stdout);
    init();int i,j;
    int low,high,mid;
    int ans;
    low=0;high=1000000000;
    ans=-1;
    while (low<=high)
    {mid=(low+high)/2;
     if (SPFA(mid))
     {ans=dist[t];
      high=mid-1;
     }
     else low=mid+1;
    }
    printf("%d",ans);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值