COGS 505. 城市
505. 城市
二分花费,然后跑最短路
spfa超时,堆优化dijkstraAC
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define maxn 10000+15 4 #define maxm 10000*5+123 5 #define pii pair<int,int> 6 #define inf 0x3f3f3f3f 7 int n,m,u,v,s,x,y,z,head[maxm],tot; 8 int c[maxn],dis[maxn],maxx; 9 10 struct Edge{ 11 int to,next,dis; 12 }edge[maxm<<1]; 13 14 inline void read(int &now) 15 { 16 char ch=getchar(); now=0; 17 while(ch>'9'||ch<'0') ch=getchar(); 18 while(ch>='0'&&ch<='9') now=now*10+ch-'0',ch=getchar(); 19 } 20 21 22 inline void add(int u,int v,int d) 23 { 24 edge[++tot].to=v; 25 edge[tot].dis=d; 26 edge[tot].next=head[u]; 27 head[u]=tot; 28 } 29 30 bool dijkstra(int mid) 31 { 32 if(c[u]>mid) return false; 33 priority_queue<pii,vector<pii>,greater<pii> >que; 34 bool vis[maxn]; 35 for(int i=1;i<=n;i++) dis[i]=inf,vis[i]=false; dis[u]=0,vis[u]=true; 36 que.push(make_pair(dis[u],u)); 37 while(!que.empty()) 38 { 39 pii cur=que.top(); que.pop(); 40 int x=cur.second; 41 vis[x]=false; 42 for(int i=head[x];i;i=edge[i].next) 43 { 44 if(c[edge[i].to]>mid) continue; 45 if(vis[edge[i].to]) continue; 46 if(dis[edge[i].to]>dis[x]+edge[i].dis) 47 { 48 dis[edge[i].to]=dis[x]+edge[i].dis; 49 que.push(make_pair(dis[edge[i].to],edge[i].to)); 50 } 51 } 52 } 53 if(dis[v]>s) return 0; 54 else return 1; 55 } 56 57 58 int Alex() 59 { 60 freopen("cost.in","r",stdin); 61 freopen("cost.out","w",stdout); 62 read(n); read(m); read(u); read(v); read(s); 63 for(int i=1;i<=n;i++) read(c[i]),maxx=max(maxx,c[i]); 64 for(int i=1;i<=m;i++) 65 { 66 read(x); read(y); read(z); 67 add(x,y,z); add(y,x,z); 68 } 69 int l=0,r=maxx; 70 while(l<r) 71 { 72 int mid=(l+r)>>1; 73 if(dijkstra(mid)) r=mid; 74 else l=mid+1; 75 } 76 if(!dijkstra(r)) 77 { 78 printf("-1\n"); 79 return 0; 80 } 81 printf("%d\n",l); 82 return 0; 83 } 84 85 int Baker=Alex(); 86 int main(){;}
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define maxn 10000+15 4 #define maxm 10000*5+123 5 #define pii pair<int,int> 6 #define inf 0x3f3f3f3f 7 int n,m,u,v,s,x,y,z,head[maxm],tot; 8 int c[maxn],dis[maxn],maxx; 9 10 struct Edge{ 11 int to,next,dis; 12 }edge[maxm<<1]; 13 14 inline void read(int &now) 15 { 16 char ch=getchar(); now=0; 17 while(ch>'9'||ch<'0') ch=getchar(); 18 while(ch>='0'&&ch<='9') now=now*10+ch-'0',ch=getchar(); 19 } 20 21 22 inline void add(int u,int v,int d) 23 { 24 edge[++tot].to=v; 25 edge[tot].dis=d; 26 edge[tot].next=head[u]; 27 head[u]=tot; 28 } 29 30 bool dijkstra(int mid) 31 { 32 if(c[u]>mid) return false; 33 priority_queue<pii,vector<pii>,greater<pii> >que; 34 bool vis[maxn]; 35 for(int i=1;i<=n;i++) dis[i]=inf,vis[i]=false; dis[u]=0,vis[u]=true; 36 que.push(make_pair(dis[u],u)); 37 while(!que.empty()) 38 { 39 pii cur=que.top(); que.pop(); 40 int x=cur.second; 41 vis[x]=false; 42 for(int i=head[x];i;i=edge[i].next) 43 { 44 if(c[edge[i].to]>mid) continue; 45 if(vis[edge[i].to]) continue; 46 if(dis[edge[i].to]>dis[x]+edge[i].dis) 47 { 48 dis[edge[i].to]=dis[x]+edge[i].dis; 49 que.push(make_pair(dis[edge[i].to],edge[i].to)); 50 } 51 } 52 } 53 if(dis[v]>s) return 0; 54 else return 1; 55 } 56 57 58 int Alex() 59 { 60 freopen("cost.in","r",stdin); 61 freopen("cost.out","w",stdout); 62 read(n); read(m); read(u); read(v); read(s); 63 for(int i=1;i<=n;i++) read(c[i]),maxx=max(maxx,c[i]); 64 for(int i=1;i<=m;i++) 65 { 66 read(x); read(y); read(z); 67 add(x,y,z); add(y,x,z); 68 } 69 int l=0,r=maxx; 70 while(l<r) 71 { 72 int mid=(l+r)>>1; 73 if(dijkstra(mid)) r=mid; 74 else l=mid+1; 75 } 76 if(!dijkstra(r)) 77 { 78 printf("-1\n"); 79 return 0; 80 } 81 printf("%d\n",l); 82 return 0; 83 } 84 85 int Baker=Alex(); 86 int main(){;}