bzoj 1614 Telephone Lines

最近最小生成树题目做傻了,看到题目的第一感觉就是最小生成树。。。

后面才发现是求最短路。

正解算法: 二分+spfa

二分枚举一个花费的钱(除去k对免费的),spfa跑最短路,如果最短路的长度大于二分值则说明是免费的。

如果免费的对数<=k 那么说明这个值是可行的 还得记录一下答案,再r=mid-1。

否则相反 l=mid+1

注意:别像我一样SPFA忘记写判环而WA。

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <queue>
 4 #include <cstring>
 5
 6
 7 struct node{
 8     int u,v,w,next;
 9     node(){}
10     node(int _u,int _v,int _w,int _next){
11         u = _u;
12         v = _v;
13         w = _w;
14         next = _next;
15     }
16 }Edge[20005];
17
18 int n,p,k,Count,head[10005];
19 int x,y,z,dis[1005],ans=-1;
20 bool inque[1005];
21
22 void AddEdge(int u,int v,int w){
23     Count++;
24     Edge[Count]=node(u,v,w,head[u]);
25     head[u]=Count;
26 }
27
28 bool spfa(int x){
29     memset(dis,0x3f3f3f3f,sizeof(dis));
30     memset(inque,0,sizeof(inque));
31     std::queue<int>Q;
32     Q.push(1);
33     int s;
34     inque[1]=1;
35     dis[1]=0;
36     while( !Q.empty() ){
37         int now = Q.front();
38         Q.pop();
39         inque[now]=0;
40         for(int i=head[now];i;i=Edge[i].next){
41             if(Edge[i].w>x) s=dis[now]+1;
42             else s = dis[now];
43             if(s<dis[Edge[i].v]){
44                 dis[Edge[i].v] = s;
45                 if(!inque[Edge[i].v]){
46                     Q.push(Edge[i].v);
47                     inque[Edge[i].v]=1;
48                 }
49             }
50         }
51     }
52     //printf("%d\n",dis[n]);
53     if(dis[n]<=k) return true;
54     return false;
55 }
56
57 int main(){
58     scanf("%d%d%d",&n,&p,&k);
59     for(int i=1;i<=p;i++){
60         scanf("%d%d%d",&x,&y,&z);
61         AddEdge(x,y,z);
62         AddEdge(y,x,z);
63     }
64     int l = 0,r = 1000000;
65     while(l<=r){
66         int mid = (l+r)>>1;
67         if( spfa(mid) ){
68             ans = mid;
69             r = mid - 1;
70         }
71         else l = mid + 1;
72     }
73     printf("%d\n",ans);
74     return 0;
75 }
时间: 2024-10-26 07:59:08

bzoj 1614 Telephone Lines的相关文章

[BZOJ] 1614: [Usaco2007 Jan]Telephone Lines架设电话线

1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1806  Solved: 773[Submit][Status][Discuss] Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N <= 1,000)根按1..N顺次编号的废

poj 3662 Telephone Lines dijkstra+二分搜索

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5696   Accepted: 2071 Description 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

bzoj1614[Usaco2007 Jan]Telephone Lines架设电话线*

bzoj1614[Usaco2007 Jan]Telephone Lines架设电话线 题意: n个节点,1号节点已经连入互联网,现在需要将整个图连入网络.有K条边可以免费连接,最后总费用为所有连边费用的最大值,求最小总费用.n≤10000 题解: 二分费用,将连边费用大于二分值的长度记为1,否则记为0,求最短路,如果到某个点的距离超过k,则需要增加答案,继续二分. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include &l

POJ 3662 Telephone Lines (分层图)

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6785   Accepted: 2498 Description 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

hust 1039 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 telep

POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 Description 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

洛谷 P1948 [USACO08JAN]电话线Telephone Lines

P1948 [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 a

poj 3662 Telephone Lines(最短路+二分)

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6973   Accepted: 2554 Description 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

POJ3662 Telephone Lines( dijkstral + 二分 )

POJ3662 Telephone Lines 题目大意:要在顶点1到顶点n之间建一条路径,假设这条路径有m条边,其中有k条边是免费的,剩余m-k条边是要收费的, 求这m-k条边中花费最大的一条边的最小花费. 让m条边中原本花费最大的k条边成为免费的边,则这时m-k条边中花费最大的一条边的花费最小. 二分枚举m-k条边中花费最大的一条边的最小花费x,dijkstra求最短路径时,将花费大于x的边的花费设为1(花费为INF的边不变),花费小于等于x的边设为 0,则d[v-1]中返回的就是花费大于x