POJ 3662 Telephone Lines(二分+最短路)
最小化第K大值。
让我怀疑人生的一题目,我有这么笨?
1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 #include <vector> 5 #include <functional> 6 using namespace std; 7 #define maxv 1010 8 #define maxl 1000000 9 struct edge 10 { 11 int to, cost; 12 edge(){} 13 edge(int to, int cost) : to(to), cost(cost){} 14 }; 15 typedef pair<int, int> P; 16 vector<edge> G[maxv]; 17 int d[maxv]; 18 int V, E; 19 int dij(int s, int x) { 20 priority_queue<P, vector<P>, greater<P> > que; 21 memset(d, 0X3f, V*sizeof(int)); 22 d[s] = 0; 23 que.push(P(0, s)); 24 while (!que.empty()) { 25 P p = que.top(); que.pop(); 26 int v = p.second; 27 if (d[v] < p.first) continue; 28 for (int i = 0; i < G[v].size(); ++i) 29 { 30 edge e = G[v][i]; 31 int new_d = d[v] + (e.cost >= x ? 1 : 0); 32 if (d[e.to] > new_d) 33 { 34 d[e.to] = new_d; 35 que.push(P(d[e.to], e.to)); 36 } 37 } 38 } 39 return d[V-1]; 40 } 41 int main(void) { 42 freopen("in.txt", "r", stdin); 43 freopen("out.txt", "w", stdout); 44 int K; 45 scanf("%d%d%d", &V, &E, &K); 46 for (int i = 0; i < E; ++i) { 47 int A, B, L; 48 scanf("%d%d%d", &A, &B, &L); 49 --A, --B; 50 G[A].push_back(edge(B,L)); 51 G[B].push_back(edge(A,L)); 52 } 53 int l = 0, u = maxl+9; 54 for (;(u-l) > 1;) { 55 int m = (u+l) >> 1; 56 if (dij(0, m) > K) { 57 l = m; 58 } else { 59 u = m; 60 } 61 } 62 printf("%d\n", (l>maxl ? -1 : l)); 63 return 0; 64 }