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,之 ...
随机推荐
- 微信小程序车牌号码模拟键盘输入
微信小程序车牌号码模拟键盘输入练习, 未经允许,禁止转载,抄袭,如需借鉴参考等,请附上该文章连接. 相关资料参考:https://blog.csdn.net/littlerboss/article/d ...
- php无限级分类----封装函数
public function catetree($cateRes){//传递过来的数据资源 return $this->sort($cateRes); 调用函数 } public functi ...
- PHPExcel 导入Excel数据 (导出下一篇我们继续讲解)
一:使用composer下载 phpoffice/phpexcel 或者直接下载安装包 composer require phpoffice/phpexcel 二 1:导入数据 原理:读取文件,获取文 ...
- 基于OMAPL138的字符驱动_GPIO驱动AD9833(三)之中断申请IRQ
基于OMAPL138的字符驱动_GPIO驱动AD9833(三)之中断申请IRQ 0. 导语 学习进入到了下一个阶段,还是以AD9833为例,这次学习是向设备申请中断,实现触发,在未来很多场景,比如做用 ...
- C++ STL lower_bound()和upper_bound()
lower_bound()和upper_bound()用法 1.在数组上的用法 假设a是一个递增数组,n是数组长度,则 lower_bound(a, a+n, x):返回数组a[0]~a[n-1]中, ...
- Python入门 (三)
迭代器与生成器 迭代器 迭代是Python最强大的功能之一,是访问集合元素的一种方式. 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器 ...
- JavaWeb——升级赛-学生成绩管理系统(2).java---19.01.03
dao.java package Dao; import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLExcept ...
- 从官网下载centos
今天想从官网下载6.5版本的CentOS,结果找了好一会儿才找到,赶紧记录下来,以备以后查询. 第一步在百度搜索centos,点击"Download CentOS",如下图所示. ...
- leetcode--笔记8 Fizz Buzz
题目要求: Write a program that outputs the string representation of numbers from 1 to n. But for multipl ...
- 「日常训练&知识学习」莫队算法(二):树上莫队(Count on a tree II,SPOJ COT2)
题意与分析 题意是这样的,给定一颗节点有权值的树,然后给若干个询问,每次询问让你找出一条链上有多少个不同权值. 写这题之前要参看我的三个blog:Codeforces Round #326 Div. ...