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,之 ...
随机推荐
- Bigdata--hadoop系列安装
Date:20180827 Monday 目前市场hadoop主流版本是2.7.x系列,下面我们就以hadoop-2.7.3为例进行安装 安装前准备: 1.操作系统:cetos(6和7) 2.java ...
- Python学习 :迭代器&生成器
列表生成式 列表生成式的操作顺序: 1.先依次来读取元素 for x 2.对元素进行操作 x*x 3.赋予变量 Eg.列表生成式方式一 a = [x*x for x in range(10)] pri ...
- Go文件处理
go语言中对文件处理一般都在os包中 func Mkdir(name string, perm FileMode) error 创建名称为name的目录,权限设置是perm,例如0777 func M ...
- C语言调整数组使奇数全部都位于偶数前面
//输入一个整数数组,实现一个函数,//来调整该数组中数字的顺序使得数组中所有的奇数 位于数组的前半部分,//所有偶数 位于数组的后半部分. #include<stdio.h>#inclu ...
- golang 兼容不同json结构体解析实践
线上服务器,同一个web接口有时需要兼容不同版本的结构体.这种情况思路是使用interface{}接收任意类型数据,结合reflect包处理. 如下,http接口调用者会传入不同的json结构数据(单 ...
- 自己用原生JS写的轮播图,支持移动端触屏滑动,面向对象思路。分页器圆点支持click和mouseover。
自己用原生javascript写的轮播图,面向对象思路,支持移动端手指触屏滑动.分页器圆点可以选择click点击或mouseover鼠标移入时触发.图片滚动用的setInterval,感觉setInt ...
- Java设计模式(22)——行为模式之状态模式(State)
一.概述 概念 再引用网友的说通俗一点: State模式在实际使用中比较多,适合"状态的切换".因为我们经常会使用If elseif else 进行状态切换, 如果针对状态的这样判 ...
- Java设计模式(21)——行为模式之备忘录模式(Memento)
一.概述 概念 UML简图 角色 根据下图得到角色 备忘录角色(Memento).发起人角色(Originator).负责人角色(Caretaker) 二.实践 使用白箱实现,给出角色的代码: 发起人 ...
- docker (2) 通用/镜像命令
原文:docker (2) 通用/镜像命令 Docker 的常用命令: (1)Docker help 命令: 可以查看有关docker的所有操作命令: (2)docker COMMAND -–hel ...
- 一个例子说明substr(), mb_substr() 和 mb_strcut()之间的区别
例子来自PHP官方文档,我只是翻译下. http://www.php.net/manual/zh/function.mb-strcut.php header( 'Content-Type:text/h ...