[USACO08JAN]电话线Telephone Lines

Posted Z-Y-Y-S

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[USACO08JAN]电话线Telephone Lines相关的知识,希望对你有一定的参考价值。

题目描述

Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of the cables required to connect his farm to the phone system.

There are N (1 ≤ N ≤ 1,000) forlorn telephone poles conveniently numbered 1..N that are scattered around Farmer John‘s property; no cables connect any them. A total of P (1 ≤ P ≤ 10,000) pairs of poles can be connected by a cable; the rest are too far apart.

The i-th cable can connect the two distinct poles Ai and Bi, with length Li (1 ≤ Li ≤ 1,000,000) units if used. The input data set never names any {Ai, Bi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and N need to be connected by a path of cables; the rest of the poles might be used or might not be used.

As it turns out, the phone company is willing to provide Farmer John with K (0 ≤ K < N) lengths of cable for free. Beyond that he will have to pay a price equal to the length of the longest remaining cable he requires (each pair of poles is connected with a separate cable), or 0 if he does not need any additional cables.

Determine the minimum amount that Farmer John must pay.

多年以后,笨笨长大了,成为了电话线布置师。由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人。该市周围分布着 N(1<=N<=1000)根据1……n顺序编号的废弃的电话线杆,任意两根线杆之间没有电话线连接,一共有p(1<=p& lt;=10000)对电话杆可以拉电话线。其他的由于地震使得无法连接。

第i对电线杆的两个端点分别是ai,bi,它们的距离为li(1<=li<=1000000)。数据中每对(ai,bi)只出现一次。 编号为1的电话杆已经接入了全国的电话网络,整个市的电话线全都连到了编号N的电话线杆上。也就是说,笨笨的任务仅仅是找一条将1号和N号电线杆连起来的 路径,其余的电话杆并不一定要连入电话网络。

电信公司决定支援灾区免费为此市连接k对由笨笨指定的电话线杆,对于此外的那些电话线,需要为它们付费,总费用决定于其中最长的电话线的长度(每根电话线仅连接一对电话线杆)。如果需要连接的电话线杆不超过k对,那么支出为0.

请你计算一下,将电话线引导震中市最少需要在电话线上花多少钱?

输入输出格式

输入格式:

输入文件的第一行包含三个数字n,p,k;

第二行到第p+1行,每行分别都为三个整数ai,bi,li。

输出格式:

一个整数,表示该项工程的最小支出,如果不可能完成则输出-1.

输入输出样例

输入样例#1: 复制
5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6
输出样例#1: 复制
4
分层图或二分答案
dist[x][k]表示x点,免费k次的费用
那么直接跑稍作修改的SPFA
二分答案直接最大生成树
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 struct Node
 8 {
 9   int next,to,dis;
10 }edge[20001];
11 struct zt
12 {
13   int x,k;
14 };
15 int n,m,k,dist[1001][1001],ans,num,head[1001],inf;
16 bool vis[1001][1001];
17 void add(int u,int v,int d)
18 {
19   num++;
20   edge[num].next=head[u];
21   head[u]=num;
22   edge[num].to=v;
23   edge[num].dis=d;
24 }
25 void SPFA()
26 {int i;
27   queue<zt>Q;
28   memset(dist,127,sizeof(dist));
29   inf=dist[0][0];
30   Q.push((zt){1,0});
31   dist[1][0]=0;
32   while (Q.empty()==0)
33     {
34       zt u=Q.front();
35       Q.pop();
36       vis[u.x][u.k]=0;
37       for (i=head[u.x];i;i=edge[i].next)
38     {
39       int v=edge[i].to;
40       if (dist[v][u.k]>max(dist[u.x][u.k],edge[i].dis))
41         {
42           dist[v][u.k]=max(dist[u.x][u.k],edge[i].dis);
43           if (vis[v][u.k]==0)
44         {
45           vis[v][u.k]=1;
46           Q.push((zt){v,u.k});
47         }
48         }
49       if (u.k+1<=k&&dist[v][u.k+1]>dist[u.x][u.k])
50         {
51           dist[v][u.k+1]=dist[u.x][u.k];
52           if (vis[v][u.k+1]==0)
53         {
54           vis[v][u.k+1]=1;
55           Q.push((zt){v,u.k+1});
56         }
57         }
58     }
59     }
60 }
61 int main()
62 {int i,x,y,d;
63   cin>>n>>m>>k;
64   for (i=1;i<=m;i++)
65     {
66       scanf("%d%d%d",&x,&y,&d);
67       add(x,y,d);
68       add(y,x,d);
69     }
70   SPFA();
71   ans=inf;
72   for (i=0;i<=k;i++)
73     ans=min(ans,dist[n][i]);
74   if (ans==inf) cout<<-1;
75   else cout<<ans;
76 }

 

以上是关于[USACO08JAN]电话线Telephone Lines的主要内容,如果未能解决你的问题,请参考以下文章

[USACO08JAN]电话线Telephone Lines

[USACO08JAN]电话线Telephone Lines

P1948 [USACO08JAN]电话线Telephone Lines

洛谷P1948[USACO08JAN]电话线Telephone Lines

P1948 [USACO08JAN]电话线Telephone Lines

[USACO08JAN]电话线Telephone Lines