ACM 第四天
A - 最短路
输入保证至少存在1条商店到赛场的路线。
Output对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间Sample Input
2 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0
Sample Output
3
2
#include<stdio.h>
#include<cstring>
const int N=,INF=;
int d[N],w[N][N],vis[N],n,m;
void dij(int src)
{
for(int i=; i<=n; i++)
d[i]=INF;
d[src]=;
memset(vis,,sizeof(vis));
for(int i=; i<=n; i++)
{
int u=-;
for(int j=; j<=n; j++)
if(!vis[j])
{
if(u==- || d[j]<d[u]) u=j;
}
vis[u]=;
for(int j=; j<=n; j++)
if(!vis[j])
{
int tmp=d[u]+w[u][j];
if(tmp<d[j]) d[j]=tmp;
}
}
}
int main()
{
int a,b,c;
while(~scanf("%d %d",&n,&m) &&( n|| m) )
{
for(int i=; i<=n; i++)
{
w[i][i]=INF;
for(int j=i+; j<=n; j++)
{
w[i][j]=w[j][i]=INF;
}
}
for(int i=; i<m; i++)
{
scanf("%d %d %d",&a,&b,&c);
w[a][b]=w[b][a]=c;
}
dij();
printf("%d\n",d[n]);
}
return ;
}
We know that there are already some roads between some villages and
your job is the build some roads such that all the villages are connect
and the length of all the roads built is minimum.
is the number of villages. Then come N lines, the i-th of which contains
N integers, and the j-th of these N integers is the distance (the
distance should be an integer within [1, 1000]) between village i and
village j.
Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then
come Q lines, each line contains two integers a and b (1 <= a < b
<= N), which means the road between village a and village b has been
built.
OutputYou should output a line contains an integer, which is the
length of all the roads to be built such that all the villages are
connected, and this value is minimum.
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
Sample Output
179
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 150
#define INF 99999999
int ma[N][N];
long long d[N],vis[N];
int n;
long long prim(int s)
{
for(int i=; i<=n; i++)
d[i]=i==s?:ma[s][i];
vis[s]=;
long long ans=;
for(int i=; i<n; i++)
{
int maxn=INF,v;
for(int j=; j<=n; j++)
if(!vis[j]&&maxn>d[j])
{
maxn=d[j];
v=j;
}
vis[v]=;
ans+=maxn;
for(int j=; j<=n; j++)
if(!vis[j]&&ma[v][j]<d[j])
d[j]=ma[v][j];
}
return ans;
}
int main()
{
int m;
int s,e;
while(~scanf("%d",&n))
{
memset(vis,,sizeof(vis));
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
scanf("%d",&ma[i][j]);
scanf("%d",&m);
while(m--)
{
scanf("%d %d",&s,&e);
ma[s][e]=;
ma[e][s]=;
}
long long ans=prim();
printf("%lld\n",ans);
}
return ;
}
ACM 第四天的更多相关文章
- ACM第四次积分赛
虽然因为第一题给的数据有问题,没能四道题都做出来,但是这次第四名,进步很大,继续努力! SAU-ACM总比赛成绩 姓名 账号 上学期成绩 第一次成绩 第二次成绩 第三次成绩 第四 ...
- ACM第四站————最小生成树(克鲁斯卡尔算法)
都是生成最小生成树,库鲁斯卡尔算法与普里姆算法的不同之处在于——库鲁斯卡尔算法的思想是以边为主,找权值最小的边生成最小生成树. 主要在于构建边集数组,然后不断寻找最小的边. 同样的题目:最小生成树 题 ...
- ACM第四站————最小生成树(普里姆算法)
对于一个带权的无向连通图,其每个生成树所有边上的权值之和可能不同,我们把所有边上权值之和最小的生成树称为图的最小生成树. 普里姆算法是以其中某一顶点为起点,逐步寻找各个顶点上最小权值的边来构建最小生成 ...
- 第一次参加acm区域赛
什么,这周天就要去参加acm焦作赛,简直不敢相信.从大一暑假七月份中旬到今天十一月23日,加入acm将近四个多月的时间,如今到了检验自己的时候了.aaaaaaaaaa.乌拉,必胜.打印个模板,在跑个步 ...
- The Parallel Challenge Ballgame[HDU1101]
The Parallel Challenge Ballgame Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- ACM 整数划分(四)
整数划分(四) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 暑假来了,hrdv 又要留学校在参加ACM集训了,集训的生活非常Happy(ps:你懂得),可是他最近 ...
- 2018.10.26 浪在ACM 集训队第四次测试赛
2018.10.26 浪在ACM 集训队第四次测试赛 题目一览表 来源 考察知识点 完成时间 A 生活大爆炸版 石头剪刀布 NOIP 提高组 2014 模拟??? 2018.11.9 B 联合 ...
- 2018牛客网暑期ACM多校训练营(第三场) A - PACM Team - [四维01背包][四约束01背包]
题目链接:https://www.nowcoder.com/acm/contest/141/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
- 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)
链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...
随机推荐
- TinyMCE插件:RESPONSIVE filemanager 9 图片自动添加水印
跟踪function() 搜索(filemanager/upload.php) 在代码中发现,上传成功后,会传回JSON信息数据,于是最后找到方法是 $upload_handler = new Upl ...
- opencv3 学习一 - Visual Studio 配置
Step 1 下载最新版的Opencv3.4.2,见图片中的网址,选择 Win Pack. Step 2 安装Opencv3 到指定目录,见图片,路径后面会用到. Step 3 把安装目录下的bin路 ...
- spark----词频统计(一)
利用Linux系统中安装的spark来统计: 1.选择目录,并创建一个存放文本的目录,将要处理的文本保存在该目录下以供查找操作: ① cd /usr/local ②mkdir mycode ③ cd ...
- WPF程序,运行时,结束时,要运行的操作(自动保存,检查单程序)
/// <summary> /// App.xaml 的交互逻辑 /// </summary> public partial class App : Application { ...
- 【转】Odoo装饰器: one装饰
one装饰器的作用是对每一条记录都执行对应的方法,相当于traditional-style中的function,无返回值! 应用举例: 定义的columns now = fields.Datetime ...
- 成都Uber优步司机奖励政策(2月28日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 单目、双目和RGB-D视觉SLAM初始化比较
无论单目.双目还是RGB-D,首先是将从摄像头或者数据集中读入的图像封装成Frame类型对象: 首先都需要将彩色图像处理成灰度图像,继而将图片封装成帧. (1) 单目 mCurrentFrame = ...
- 利尔达NB-IOT模组Coap数据AT+NMGS发送时返回-513的原因
1. 利尔达NB-IOT模组使用AT+NMGS发送数据,返回-513的问题,大致有3种可能性,在硬件上,模组的射频电路分为A型和B型模组,所以烧写固件的时候,也要分为A和B型固件,如果烧写反了,那么R ...
- 这样的SQL居然能执行
select /*! distinct cities.id from cities join countries on cities.id = countries.id limit 10 */;
- 「日常训练」Paths and Trees(Codeforces Round 301 Div.2 E)
题意与分析 题意是这样的,定义一个从某点出发的所有最短路方案中,选择边权和最小的最短路方案,称为最短生成树. 现在求一棵最短生成树,输出总边权和与选取边的编号. 我们首先要明白这样一个结论:对一个图求 ...