| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
首次刷入第一页!AC code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=1005;
const int maxm=10010;
int n,P,K,L,Tmax;
int E[2*maxm],w[2*maxm],p[2*maxm],pre[2*maxm],fst[maxn],dist[maxn],vis[maxn],q[maxn+10];
void add(int a,int b,int c)
{
E[++L]=b; w[L]=c; pre[L]=fst[a]; fst[a]=L;
}
void init()
{
int a,b,c; L=Tmax=0;
scanf("%d%d%d",&n,&P,&K);
for(int i=1;i<=P;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c); add(b,a,c); Tmax=max(Tmax,c);
}
}
bool spfa(int v)
{
for(int i=1;i<=L;i++) p[i]=w[i]<=v?0:1;
int head,tail;
memset(vis,0,sizeof(vis));
memset(dist,127,sizeof(dist));
head=1; tail=2; q[1]=1; dist[1]=0; vis[1]=1;
while(head!=tail)
{
int u=q[head];
for(int k=fst[u];k;k=pre[k])
if(dist[u]+p[k]<dist[E[k]])
{
dist[E[k]]=dist[u]+p[k];
if(!vis[E[k]]) {vis[E[k]]=1; q[tail++]=E[k]; if(tail>n) tail-=n;}
}
vis[u]=0; head++; if(head>n) head-=n;
}
return(dist[n]<=K);
}
void work()
{
int low=0,high=Tmax+1;
while(low<high)
{
int mid=(low+high)/2;
if(spfa(mid)) high=mid; else low=mid+1;
}
if((low+high)/2>Tmax) printf("-1\n"); else printf("%d\n",(low+high)/2);
}
int main()
{
init();
work();
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator