标签:
Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务。于是,FJ必须为此向电信公司支付一定的费用。
输出说明:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> #include<queue> using namespace std; const int maxn = 20005,maxint = 0x3fffffff; struct edge{ int v; int w; }; int n,p,k,dis[maxn],mx; bool inq[maxn]; vector<edge> g[maxn]; void input(){ cin>>n>>p>>k; int u,v,w; edge tmp; mx = 0; for(int i = 1;i <= p;i++){ scanf("%d%d%d",&u,&v,&w); tmp.v = v; tmp.w = w; g[u].push_back(tmp); tmp.v = u; g[v].push_back(tmp); mx = max(mx,w); } } void spfa(int t){ memset(inq,false,sizeof(inq)); for(int i = 1;i <= n;i++) dis[i] = maxint; queue<int> q; inq[1] = true; dis[1] = 0; q.push(1); while(!q.empty()){ int u = q.front(),v,w; inq[u] = false; q.pop(); for(int i = 0;i < g[u].size();i++){ v = g[u][i].v; w = g[u][i].w > t ? 1 : 0; if(dis[u] + w < dis[v]){ dis[v] = dis[u] + w; if(!inq[v]){ inq[v] = true; q.push(v); } } } } } bool check(int t){ spfa(t); if(dis[n] <= k){ return true; } else return false; } void div(){ int lans = 0,rans = mx,mans; while(lans <= rans){ mans = (lans + rans) >> 1; if(check(mans)){ rans = mans - 1; }else{ lans = mans + 1; } } if(check(mans)){ if(mans > mx) cout<<-1; else cout<<mans; }else{ if(mans + 1> mx) cout<<-1; else cout<<mans + 1; } } int main(){ input(); div(); return 0; }
标签:
原文地址:http://www.cnblogs.com/hyfer/p/5656494.html