[Cogs] 147. [usaco jan08] Build a telephone line (Binary + spfa)

Source: Internet
Author: User

Http://cojs.tk/cogs/problem/problem.php? PID = 1, 147

I learned a new posture, orz.

This question is a 1 ~ The maximum path of N is the smallest.

Of course it is outside K.

We can convert it.

Evaluate the number of paths greater than a certain value. If the number is smaller than K, this must be a feasible solution. Because other edges are less than this value. (Of course, the price is required here)

When finding this path, we must satisfy this path to be the smallest side than this value. Of course, we must use the shortest path.

Then the problem is converted to, given a value, evaluate 1 ~ A path of N meets the minimum value of edge, and less than K.

Then we can give two answers. (Of course, the answer is monotonous .)

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <string>using namespace std;#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << #x << " = " << x << endl#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }#define setio(x) freopen(""#x".in", "r", stdin); freopen(""#x".out", "w", stdout)inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }using namespace std;const int N=1005;int d[N], ihead[N], cnt, vis[N], q[N], tail, front, n, m, k;struct ED { int to, next, w; } e[N*N];inline void add(const int &u, const int &v, const int &w) {e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].w=w;}inline const bool spfa(const int &x) {CC(d, 0x3f);d[1]=tail=front=0; vis[1]=1; q[tail++]=1;int u, v, t;while(front!=tail) {u=q[front++]; if(front==N) front=0; vis[u]=0;for(int i=ihead[u]; i; i=e[i].next) {v=e[i].to;if(e[i].w>x) t=d[u]+1;else t=d[u];if(t<d[v]) {d[v]=t;if(!vis[v]) { vis[v]=1; q[tail++]=v; if(tail==N) tail=0; }}}}return d[n]<=k;}int main() {setio(phoneline);read(n); read(m); read(k);int u, v, w;while(m--) {read(u); read(v); read(w);add(u, v, w); add(v, u, w);}int l=0, r=1000000, mid;while(l<=r) {mid=(l+r)>>1;if(spfa(mid)) r=mid-1;else l=mid+1;}if(l>1000000) puts("-1");else print(r+1);return 0;}

 

 

 

 

Farmer John planned to direct the telephone line to his farm, but the telecommunications company did not plan to provide him with free services. Therefore, FJ must pay a certain fee to the telecommunications company.

There are N (1 <= n <= 1,000) roots distributed around the farm of FJ .. N sequential number of discarded telephone lines, any two telephone lines are not connected to the telephone line. A total of P (1 <= P <= 10,000) can be used to pull telephone lines between telephone lines, and others cannot be connected due to the distance.

The two ends of the I-th telephone pole are A_ I and B _ I, respectively, and the distance between them is L_ I (1 <= L_ I <= 1,000,000 ). Ensure that each pair of {a_ I, B _ I} appears only once at most. The telephone pole numbered 1 has been connected to the national telephone network, and all the telephone lines on the farm are connected to the telephone pole numbered n. That is to say, the task of Fj is only to find a path to connect telephone lines 1 and N. Other telephone lines do not have to be connected to the telephone network.

After negotiation, the telecommunications company eventually agreed to connect K (0 <= k <n) to the telephone pole specified by FJ for free. For other telephone lines, FJ will pay for them, equal to the length of the longest telephone line (each telephone line is associated with only one pair of telephone lines ). If the number of telephone lines to be connected does not exceed K pairs, the total cost of Fj is 0.

Calculate the minimum cost of Fj over the telephone line.

Program name: PHONELINE

Input Format:

  • Row 3: Three integers separated by spaces: N, P, and K
  • 2nd. p + 1 row: I + 1 Act 3 integers separated by spaces: a_ I, B _ I, L_ I

Input sample (PHONELINE. In ):

5 7 11 2 53 1 42 4 83 2 35 2 93 4 74 5 6

Input description:

A total of five discarded telephone lines. Telephone Line 1 cannot be directly connected to telephone lines 4 or 5. Telephone Line 5 cannot be directly connected to telephone lines 1 and 3. Telephone lines can be used between all other telephone lines. Telecom companies can connect one pair of telephone lines to FJ for free.

Output Format:

  • Row 3: Output 1 integer, which is the minimum expenditure of Fj on this project. If the task cannot be completed, output-1

Output sample (PHONELINE. out ):

4

Output description:

FJ select the following link scheme: 1-> 3; 3-> 2; 2-> 5, the telephone lines required for the three telephone lines are 4, 3, and 9 respectively. FJ asked the telecommunications company to provide the telephone line with a length of 9, so the maximum length of the telephone line he needs to buy is 4.

[Cogs] 147. [usaco jan08] Build a telephone line (Binary + spfa)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.