标签:思路 gre 策略 ems 多少 pop pac one [1]
\[ O((N+M)logNlogAns) \]
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define maxn 1001
#define maxm 10001
using namespace std;
struct edge{
int to,next,dis;
edge(){}
edge(const int &_to,const int &_dis,const int _next){
to=_to,dis=_dis,next=_next;
}
}e[maxm<<1];
int head[maxn],k;
int dis[maxn],maxlen,ans;
bool vis[maxn];
int n,m,t;
priority_queue< pair<int,int>,vector< pair<int,int> >,greater< pair<int,int> > > q;
inline int read(){
register int x(0),f(1); register char c(getchar());
while(c<'0'||'9'<c){ if(c=='-') f=-1; c=getchar(); }
while('0'<=c&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
inline void add(const int &u,const int &v,const int &w){
e[k]=edge(v,w,head[u]);
head[u]=k++;
}
inline void dijkstra(const int &lim){
memset(dis,0x3f,sizeof dis);
memset(vis,false,sizeof vis);
q.push(make_pair(0,1));
dis[1]=0;
while(q.size()){
int u=q.top().second; q.pop();
if(vis[u]) continue; vis[u]=true;
for(register int i=head[u];~i;i=e[i].next){
int v=e[i].to;
if(dis[v]>dis[u]+(e[i].dis>lim))
dis[v]=dis[u]+(e[i].dis>lim),q.push(make_pair(dis[v],v));
}
}
}
int main(){
memset(head,-1,sizeof head);
n=read(),m=read(),t=read();
for(register int i=1;i<=m;i++){
int u=read(),v=read(),w=read();
add(u,v,w),add(v,u,w);
if(maxlen<w) maxlen=w;
}
int l=0,r=maxlen,flag=false;
while(l<=r){
int lim=l+r>>1;
dijkstra(lim);
if(dis[n]<=t) flag=true,ans=lim,r=lim-1;
else l=lim+1;
}
printf("%d\n",flag?ans:-1);
return 0;
}
[Usaco2007 Jan]Telephone Lines架设电话线
标签:思路 gre 策略 ems 多少 pop pac one [1]
原文地址:https://www.cnblogs.com/akura/p/10848893.html