输入格式

第一行三个整数 N,P,KN,P,K.

接下来 PP 行,每行三个整数 A_i,B_i,L_iAi​,Bi​,Li​.

输出格式

若不存在从 11 到 NN 的路径,输出 -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

样例输出

4

数据范围

题解:

  • 对于 50Pts , 考虑直接暴力搜索.
  • 对于 100Pts , 本题的答案显然存在单调性。因此考虑使用二分+最短路。二分一个Mid值使得花费不超过Mid.我们只需要把价格大于Mid的看为1边,不超过的看为0边,然后求1-N的最短路是否不超过K即可.
  • 似乎还有一种使用动态规划+最短路进行求解的方法,目前还未研究,有兴趣的可以进行了解。

代码片:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<map>
 8 #include<set>
 9 #include<vector>
10 #include<queue>
11 #define Res register
12 #define Inf 1e9
13 const int Num = 2000+200;
14
15 using namespace std;
16
17 int N , M , K , An = Inf , Sum = 0;
18 struct Yuns{
19     int X , Y , Z;
20 }Ver[Num];
21 int Ans[Num] , Next[Num*2] , Head[Num*2] , Book[Num] , Que[Num];
22
23 inline int Read()
24 {
25     int x=0,f=1;char ch=getchar();
26     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
27     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
28     return x*f;
29 }
30
31 void Add(int X , int Y , int Z){
32     Ver[++Sum].X = X;
33     Ver[Sum].Y = Y;
34     Ver[Sum].Z = Z;
35     Next[Sum] = Head[X];
36     Head[X] = Sum;
37     return;
38 }
39
40 void Dfs(int Line , int Step){
41
42     if (Line == N){
43         for (Res int i = 1 ; i <= Step ; i++) Que[i] = Ans[i];
44         sort(Que+1 , Que+Step+1);
45         An = min(An , Que[Step-K]);
46         memset(Que , 0 , sizeof(Que));
47         return;
48     }
49
50     for (Res int i = Head[Line] ; i ; i = Next[i]){
51         int Y = Ver[i].Y;
52         if (!Book[Y]){
53             Book[Y] = 1;
54             Ans[Step] = Ver[i].Z;
55             Dfs(Y , Step+1);
56             Ans[Step] = 0;
57             Book[Y] = 0;
58         }
59
60     }
61     return;
62 }
63
64 int main(){
65
66     int A , B , C;
67     N = Read() , M = Read() , K = Read();
68     for (Res int i = 1 ; i <= M ; i++){
69         A = Read() , B = Read() , C = Read();
70         Add(A , B , C) , Add(B , A , C);
71     }
72
73     Dfs(1 , 1);
74
75     if (An == 1e9){
76         cout << -1;
77         return 0;
78     }else cout << An;
79
80     return 0;
81
82 }

50Pts

 1 #include <cstdio>
 2 #include <vector>
 3 #include <queue>
 4 #include <algorithm>
 5 using namespace std;
 6 const int inf = 1000 + 10;
 7 const int maxn = 1000 + 10;
 8 const int maxp = 10000 + 10;
 9 const int maxl = 1000000 + 10;
10 typedef pair<int, int> P;
11 struct pole {
12     int b, l;
13     pole() {}
14     pole(int b, int l) : b(b), l(l) {}
15 };
16 vector<pole> G[maxp];
17 int d[maxn];
18 int n, p, k;
19 int Dijkstra(int s, int x) {
20     priority_queue<P, vector<P>, greater<P> > que;
21     fill(d, d + n, inf);
22     d[s] = 0;
23     que.push(P(0, s));
24     while (!que.empty()) {
25         P p = que.top();
26         que.pop();
27         int v = p.second;
28         if (d[v] < p.first)
29             continue;
30         for (int i = 0; i < G[v].size(); i++) {
31             pole e = G[v][i];
32             int nd = d[v] + (e.l > x ? 1 : 0);
33             if (d[e.b] > nd) {
34                 d[e.b] = nd;
35                 que.push(P(d[e.b], e.b));
36             }
37         }
38     }
39     return d[n-1];
40 }
41 int main(int argc, char const *argv[]) {
42     scanf("%d%d%d", &n, &p, &k);
43     for (int i = 0; i < p; i++) {
44         int a, b, l;
45         scanf("%d%d%d", &a, &b, &l);
46         a--, b--;
47         G[a].push_back(pole(b, l));
48         G[b].push_back(pole(a, l));
49     }
50     int lb = 0, ub = maxl;
51     while (ub > lb) {
52         int mid = (lb + ub) / 2;
53         if (Dijkstra(0, mid) > k) {
54             lb = mid + 1;
55         } else {
56             ub = mid;
57         }
58     }
59     if (lb == maxl) {
60         puts("-1");
61     } else {
62         printf("%d\n", ub);
63     }
64     return 0;
65 }

100Pts

转载于:https://www.cnblogs.com/Yuns/p/10370075.html

「LOJ/一本通 3.2 例 3」架设电话线相关推荐

  1. 「一本通 3.2 例 3」架设电话线

    题目大意 在加权无向图上求出一条从  号结点到  号结点的路径,使路径上第  大的边权尽量小. 思路 由于是一次性的,且这题数据极小,考虑 正常情况下是来更新数组的,不过这次是更新 表示第个节点,(可 ...

  2. 【C++】「一本通 1.1 例 4」加工生产调度

    「一本通 1.1 例 4」加工生产调度 [来源] [题目描述] [输入格式] [输出格式] [输入样例] [输出样例] [数据范围] [解析] [代码] [来源] 一本通题库-1425 LibreOJ ...

  3. LibreOJ10082. 「一本通 3.3 例 1」Word Rings【二分+SPFA】

    10082. 「一本通 3.3 例 1」Word Rings [题目描述] 传送门 [题解] 将一个字符串看成一条边,字符两端的字符看成节点,长度看成权值.二分枚举答案,最后SPFA刷正环,因为只要有 ...

  4. 【C++】「一本通 1.1 例 2」种树

    「一本通 1.1 例 2」种树 [来源] [题目描述] [输入格式] [输出格式] [输入样例] [输出样例] [解析] [代码] [来源] 一本通题库-1423 LibreOJ-10001 vjud ...

  5. #10001. 「一本通 1.1 例 2」种树

    #10001. 「一本通 1.1 例 2」种树 满足n个区间种树的要求,求最少种多少棵数 思路 按照区间的尾巴来排序,因为如果区间有重叠的种在第一个区间的尾巴可以使得种树更少,所有每次始从尾巴开始种树 ...

  6. 【C++】「一本通 1.1 例 5」智力大冲浪

    「一本通 1.1 例 5」智力大冲浪 [来源] [题目描述] [输入格式] [输出格式] [输入样例] [输出样例] [数据范围] [解析] [代码] [来源] 一本通题库-1426 LibreOJ- ...

  7. Loj 10115 「一本通 4.1 例 3」校门外的树 (树状数组)

    题目链接:https://loj.ac/problem/10115 题目描述 原题来自:Vijos P1448 校门外有很多树,学校决定在某个时刻在某一段种上一种树,保证任一时刻不会出现两段相同种类的 ...

  8. LOJ #10222. 「一本通 6.5 例 4」佳佳的 Fibonacci

    题目链接 题目大意 $$F[i]=F[i-1]+F[i-2]\ (\ F[1]=1\ ,\ F[2]=1\ )$$ $$T[i]=F[1]+2F[2]+3F[3]+...+nF[n]$$ 求$T[n] ...

  9. LOJ #10155. 「一本通 5.2 例 3」数字转换

    无向图的最长链怎么求?和树的直径求法相同. #include <bits/stdc++.h> using namespace std; const int N=5e4+5; int n,a ...

最新文章

  1. mysql 中float存入int数据显示失真问题
  2. Firefox 火狐网址生成二维码扩展推荐
  3. Word排版艺术—读书笔记
  4. NPDP国际产品经理认证是什么?看完你就懂了
  5. linux设置家目录,usermod更改用户家目录
  6. 14 ABSOLUTE评估肿瘤纯度
  7. 怎么用pr(Premiere)给视频添加水印
  8. java 生成ppt_Java 创建并应用幻灯片母版
  9. 如何在ios手机端的Safari浏览器 中“查看网页源代码”
  10. 用python画皮卡丘画法-用python画一只可爱的皮卡丘实例
  11. go 错误处理与测试
  12. Cannot commit, transaction is already closed
  13. 俞敏洪在北京大学2008年开学典礼上的演讲辞
  14. 顺序表的基本操作(详细、全面)
  15. C语言实现获取文件后缀、修改后缀
  16. 计算机主机突然断电有什么影响,台式机突然断电有什么危害
  17. 腾讯 地图 机器学习岗 春招实习123面(猝)
  18. 蓝屏的硬件原因及解决
  19. HPE还将继续支持下一代Superdome GPU芯片
  20. Windows Server 2016-Active Directory域服务端口汇总

热门文章

  1. lan pci 联想开机_电脑开机出现PCI LAN是什么情况
  2. RESTEasy 参数注解
  3. 骨干网络对比-EfficientNet-Lite
  4. c语言经典程序 流程图,c语言超经典矩阵式键盘的接法,流程图和扫描程序
  5. 解决MongoDB报错:Cannot create collection “None“ as a capped collection as it already exist
  6. GEE:MODIS计算生物量
  7. IntelliJ IDEA 的 Metamodel 配置
  8. 科奥斯扫地机器人怎么样_科沃斯DL33max扫地机器人怎么样?不是忽悠,真实情况分享!sundhaow...
  9. 数据包覆盖Android,安卓数据包怎么安装 安卓游戏数据包安装教程
  10. unsigned int用法