标签:
Time Limit: 10000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1790 Accepted Submission(s):
577
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <algorithm> 6 #define INF 0x3f3f3f3f 7 #define M 50005 8 #define N 10005 9 using namespace std; 10 11 int tol,n,m,t,limit; 12 struct Edge 13 { 14 int form,to,val,time; 15 int next; 16 } edge[M*2]; 17 18 int head[M*2],dis[N],r[M]; 19 bool vis[N]; 20 21 bool cmp(int a,int b) 22 { 23 return a>b; 24 } 25 26 void init() 27 { 28 tol=0; 29 memset(head,-1,sizeof(head)); 30 } 31 32 void addEdge(int u,int v,int val,int time) ///邻接表 33 { 34 edge[tol].form=u; 35 edge[tol].to=v; 36 edge[tol].val=val; 37 edge[tol].time=time; 38 edge[tol].next=head[u]; 39 head[u]=tol++; 40 edge[tol].form=v; 41 edge[tol].to=u; 42 edge[tol].val=val; 43 edge[tol].time=time; 44 edge[tol].next=head[v]; 45 head[v]=tol++; 46 } 47 48 void getmap() 49 { 50 scanf("%d%d%d",&n,&m,&t); 51 int a,b,c,d; 52 for(int i=0; i<m; i++) 53 { 54 scanf("%d%d%d%d",&a,&b,&c,&d); 55 r[i]=c; 56 addEdge(a,b,c,d); 57 } 58 sort(r,r+m,cmp); ///从大到小排序 59 60 } 61 62 int spfa() ///求最短时间 63 { 64 memset(dis,INF,sizeof(dis)); 65 memset(vis,false,sizeof(vis)); 66 queue<int>q; 67 q.push(1); 68 dis[1]=0; 69 vis[1]=true; 70 while(!q.empty()) 71 { 72 int u=q.front(); 73 q.pop(); 74 vis[u]=false; 75 for(int i=head[u]; i!=-1; i=edge[i].next) 76 { 77 int v=edge[i].to; 78 if(edge[i].val>=limit) 79 if(dis[v]>dis[u]+edge[i].time) 80 { 81 dis[v]=dis[u]+edge[i].time; 82 if(!vis[v]) 83 { 84 vis[v]=true; 85 q.push(v); 86 } 87 } 88 } 89 } 90 return dis[n]; 91 } 92 93 94 95 void search() 96 { 97 int left=0,right=m-1,mid; 98 while(left<right) ///二分 99 { 100 mid=(left+right)/2; 101 limit=r[mid]; 102 int tmp=spfa(); 103 if(tmp==INF||tmp>t) 104 left=mid+1; 105 else 106 right=mid; 107 } 108 printf("%d\n",r[left]); 109 } 110 111 int main() 112 { 113 int T; 114 scanf("%d",&T); 115 while(T--) 116 { 117 init(); 118 getmap(); 119 search(); 120 } 121 return 0; 122 }
hdu 1839 Delay Constrained Maximum Capacity Path(spfa+二分)
标签:
原文地址:http://www.cnblogs.com/pshw/p/5749772.html