目录

XP的点滴:

丛林小道

How Many Tables

Dijkstra

寻找最大数

最短路径问题


XP的点滴:

https://blog.csdn.net/Y_yunhu/article/details/84714635

丛林小道

题目描述


The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.

The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.

The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.

输入

 

9 A 2 B 12 I 25 B 3 C 10 H 40 I 8 C 2 D 18 G 55 D 1 E 44 E 2 F 60 G 38 F 0 G 1 H 35 H 1 I 35 3 A 2 B 10 C 40 B 1 C 20 0

输出

 

216 30

样例输入 Copy

9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0

样例输出 Copy

216
30
#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
typedef struct mystruct{int sta,fin;int v;
};
mystruct e[1005];
int fa[1005];
int r[1005];
int n,m,ans,sum;
bool cmp(mystruct a,mystruct b){return a.v<b.v;
}
void mset(){for(int i=0;i<=ans;++i){fa[i]=i;r[i]=0;}
}
int fset(int x){if(fa[x]!=x){fa[x]=fset(fa[x]);}return fa[x];
}
void Krusal(){int i,j;mset();for(i=0;i<ans;++i){int aa=fset(e[i].sta);int bb=fset(e[i].fin);if(aa==bb){continue;}if(r[aa]>r[bb]){fa[bb]=fa[aa];}else{fa[aa]=fa[bb];if(r[aa]==r[bb]){++r[bb];}}sum+=e[i].v;}
}
int main(){int t;int i,j;char x,y;int a,b;while(cin>>t&&t){ans=0;sum=0;for(i=0;i<t-1;++i){cin>>x>>a;for(j=0;j<a;++j){cin>>y>>b;e[ans].sta=x-'A';e[ans].fin=y-'A';e[ans++].v=b;}}sort(e,e+ans,cmp);Krusal();cout<<sum<<endl;}return 0;
}

How Many Tables

题目描述

Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.

输入

The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.

输出

For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.

样例输入 Copy

2
5 3
1 2
2 3
4 55 1
2 5

样例输出 Copy

2
4
#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
int fa[1005],r[1005];
int n,m,ans;
void mset(){for(int i=0;i<=n;++i){fa[i]=i;r[i]=0;}
}
int fset(int x){if(fa[x]!=x){fa[x]=fset(fa[x]);}return fa[x];
}
void uset(int a,int b){int aa=fset(a);int bb=fset(b);if(aa==bb){return ;}ans--;if(r[aa]>r[bb]){fa[bb]=fa[aa];}else{fa[aa]=fa[bb];if(r[aa]==r[bb]){++r[aa];}}
}
int main(){int t;int i,j;while(cin>>t){while(t--){int x,y;cin>>n>>m;mset();ans=n;for(i=0;i<m;++i){cin>>x>>y;uset(x,y);}cout<<ans<<endl;}}return 0;
}

Dijkstra

题目描述

编程实现Dijkstra算法

输入

第1行第1个值表示顶点个数,第2个值表示边个数;第2行开始为边及权重

输出

顶点0到每一个顶点的最短路径长度

样例输入 Copy

5 7
0 1 10
0 3 30
0 4 100
1 2 50
2 4 10
3 2 20
3 4 60

样例输出 Copy

0 10 50 30 60
#include<bits/stdc++.h>
using namespace std;
#define maxn 99999999
int e[1005][1005];
int vis[1005];
int dis[1005];
int n, m;
int u;
void mset() {memset(vis, 0, sizeof(vis));for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {if (i == j) {e[i][j] = 0;} else {e[i][j] = maxn;}}}
}
void dijkstra() {int i, j;for (i = 1; i <= n - 1; i++) {int Min = maxn;// 寻找权值最小的点ufor (j = 1; j <= n; j++) {if (vis[j] == 0 && dis[j] < Min) {Min = dis[j];u = j;}}vis[u] = 1;for (j = 1; j <= n; j++) {if (!vis[j] && e[u][j] < maxn) {if (dis[j] > dis[u] + e[u][j]) {dis[j] = dis[u] + e[u][j];}}}}for (int i = 1; i <= n; i++) {cout << dis[i] << " ";}cout << endl;
}
int main() {int a, b, c;int i;while (cin >> n >> m) {u = 0;mset();for (i = 1; i <= m; i++) {cin >> a >> b >> c;++a;++b;e[a][b] = c;}for (i = 1; i <= n; i++) {dis[i] = e[1][i];}vis[1] = 1;dijkstra();}return 0;
}

寻找最大数

题目描述

给出一个正整数N,每次可以移动2个相邻数位上的数字,最多移动K次,得到一个新的正整数。求这个新的正整数的最大值。

输入

输入一个正整数N和K,输出新的正整数。例如:N=1990,K=1,输出9190;N=101,K=0,输出101;N= 9090000078001234,K= 6,输出9907000008001234。

输出

输出新的数字

样例输入 Copy

1990 1
101 0
9090000078001234 6

样例输出 Copy

9190
101
9907000008001234
#include<bits/stdc++.h>
using namespace std;
int main(){string s;int n;int i,j;int len;while(cin>>s>>n){int ans=n;int index;index=0;len=s.length();if(len==1||n==0){cout<<s<<endl;continue;}for(i=0;i<len&&ans!=0;++i){char maxc=s[i];for(j=i+1;j<i+ans+1&&j<len;++j){if(s[j]>maxc){index=j;maxc=s[j];}}if(maxc!=s[i]){for(j=index;j>i;j--){swap(s[j],s[j-1]);}ans=ans-index+i;}}cout<<s<<endl;}return 0;
}

最短路径问题

题目描述

给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。

输入

输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点。n和m为0时输入结束,(n <= 100,保证给定的图一定是连通的)

输出

输出 一行有两个数, 最短距离及其花费。

样例输入 Copy

3 2
1 2 5 6
2 3 4 5
1 3
0 0

样例输出 Copy

9 11
#include<bits/stdc++.h>
using namespace std;
#define maxn 99999999
int e[1005][1005];
int vis[1005];
int dis[1005];
int sum[1005];
int val[1005][1005];
int n, m,s,t;
void mset() {for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {if (i == j) {e[i][j] = 0;val[i][j]=0;} else {e[i][j] = maxn;val[i][j]=maxn;}}}
}
void dijkstra() {int i, j;memset(vis, 0, sizeof(vis));vis[s] = 1;dis[s]=0;sum[s]=0;for (i = 1; i <= n; i++) {dis[i] = e[s][i];sum[i]=val[s][i];}int u;for (i = 1; i < n; i++) {int Min = maxn;for (j = 1; j <= n; j++) {if (vis[j] == 0 && dis[j] < Min) {Min = dis[j];u = j;}}vis[u] = 1;for (j = 1; j <= n; j++) {if (vis[j]==0 && e[u][j] < maxn) {if (dis[j] > dis[u] + e[u][j]) {dis[j] = dis[u] + e[u][j];sum[j] = sum[u] + val[u][j];}else if(dis[j] == dis[u] + e[u][j]&&sum[j] > sum[u] + val[u][j]){sum[j] = sum[u] + val[u][j];}}}}cout<<dis[t]<<" "<<sum[t]<<endl;
}
int main() {int a, b, c,d;int i;while (cin >> n >> m&&(m||n)) {mset();for (i = 1; i <= m; i++) {cin >> a >> b >> c>>d;if(e[a][b]>c){e[a][b] =e[b][a]= c;val[a][b]=val[b][a]=d;}else if(e[a][b]==c&&val[a][b]>d){val[a][b]=val[b][a]=d;}}cin>>s>>t;dijkstra();}return 0;
}

计科练习13题解(最短路径)相关推荐

  1. 计科练习12题解(并查集,最小生成树)

    目录 黑暗意志 闪闪发光 最小生成树(Prim) 并查集 隔离 最小生成树(Kruskal) 黑暗意志 题目描述 在数千年前潘达利亚从卡利姆多分离之时,迷雾笼罩着这块新形成的大陆,使它不被外来者发现. ...

  2. java第六次作业 计科1501班 张鹏

    java 第六次作业 计科1501 张鹏 1.用思维导图对本周的学习内容进行总结. 2.当程序中出现异常时,JVM会依据方法调用顺序依次查找有关的错误处理程序.可使用printStackTrace和g ...

  3. 22考研上岸西电计科初试395分经验分享

    [西电22考研 计科院834]17级毕业生 本科双非通信专业 脱产在家 辞职跨考 一战上岸西安电子科技大学! 视频原地址 备考经验分享视频 接下来我将从以下几个方面简单的介绍一下我自己,希望准备考研的 ...

  4. 18计科专业《Java程序设计》教学大纲

    18计科专业<Java程序设计>教学大纲 课程代码: 适用专业:计算机科学与技术 执 笔 人: 审 核 人: 学分学时:2学分40学时 制订时间:2019年12月 一.课程定位 本课程是一 ...

  5. 18计科专业《数据结构》教学大纲

    18计科专业<数据结构>教学大纲 课程代码: 适用专业:计算机科学与技术 执 笔 人: 审 核 人: 学分学时:2学分40学时 制订时间:2019年12月 一.课程定位 本课程是一门以理论 ...

  6. c语言实验设计报告,c语言实验设计报告计科学号.doc

    c语言实验设计报告计科学号.doc C语言程序设计课程实验报告系 院 信息工程学院 班 级 计科1101 学 号 111404107 姓 名 杜舒静 完成日期 2012年2-6月 指导教师 徐永安 二 ...

  7. 【计组理论期末考试模拟题】21级计科专业计算机组成原理

    [计组理论期末考试模拟题]21级计科专业计算机组成原理 一.选择题 二.多选题 三.填空题 四.程序填空题 五.编程题 一.选择题 2-1 在定点二进制运算器中,减法运算一般通过()来实现. A.原码 ...

  8. 张萍萍 计科高职13-1 201303014010 实践三结对项目

    实践三结对项目   学号: 201303014010   姓名:张萍萍    班级:计科(高职)13-1 一.题目简介: 设计一个名为MyPoint的类,表示一个带x坐标和y坐标的点.该类包括: 两个 ...

  9. 衡阳技师学校计算机系,2017级计科师范专业到衡阳技师学院见习

    2019年10月21日,由指导老师郑光勇.李康满带领的2017级计科师范专业同学组成的见习团,前往衡阳技师学院进行见习活动. 顺利抵达目的地后,首先召开了见习工作会议.本次会议由衡阳技师学院信息与生物 ...

最新文章

  1. 哈希表的分类,创建,查找 以及相关问题解决
  2. JS+CSS控制左右切换鼠标可控的无缝图片滚动代码
  3. 985高校挖出古墓!网友:毕业论文自己找上门来了
  4. vuex的命名空间有哪些_vuex模块化和命名空间的实例代码
  5. 关于ST02看到SWAP红色的讨论
  6. java usc2短信编码_手机短信PDU编码与解码
  7. Linux(树莓派)安装 python-opencv
  8. 使用一下SQL Server 2008中的新日期函数
  9. 2021-08-3126. 删除有序数组中的重复项 数组
  10. 物联网之NB-IoT技术实践开发二
  11. [相关工具] Flash CC 破解版下载
  12. 大小限制_微信传文件有大小限制?PPT太大怎么传?PPT自带三大压缩功能
  13. 360与腾讯弹窗大战 数亿网民被迫围观
  14. 第八节课-深度学习软件
  15. 手把手Verilog HDL同步Vaild-Ready握手FIFO机制
  16. Chromedriver Mirror
  17. Elasticsearch(ES) 基本知识
  18. 二本计算机类专业农村学生出路,农村“二本”大学生有哪些出路?主要出路有四条,第2条可逆袭...
  19. SegmentFault 技术周刊 Vol.4 - 这份 Android 有点甜
  20. 企业移动应用管理平台 - MDM,MAM,MCM

热门文章

  1. Kali linux修改源文件
  2. 探究JVM(一)JDK 8 以后内存区域的变化
  3. ESD器件的主要性能参数
  4. cocos2dx-2.1.5-孤狼优化版
  5. 树莓派存储方案_树莓派自建 NAS 云盘之——树莓派搭建网络存储盘
  6. rust:读取并显示文本文件
  7. Maix Bit(K210) 裸机开发教程(二)串口通信
  8. Java 计算任意两天之间相隔的天数
  9. and true和if都是python语言的保留字_python语言保留字有哪些
  10. MarkdownPad 2 Pro 注册码