A - 最短路

在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?

Input输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。

输入保证至少存在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 ;
}
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

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.

InputThe first line is an integer N (3 <= N <= 100), which
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 第四天的更多相关文章

  1. ACM第四次积分赛

    虽然因为第一题给的数据有问题,没能四道题都做出来,但是这次第四名,进步很大,继续努力! SAU-ACM总比赛成绩       姓名     账号  上学期成绩 第一次成绩 第二次成绩 第三次成绩 第四 ...

  2. ACM第四站————最小生成树(克鲁斯卡尔算法)

    都是生成最小生成树,库鲁斯卡尔算法与普里姆算法的不同之处在于——库鲁斯卡尔算法的思想是以边为主,找权值最小的边生成最小生成树. 主要在于构建边集数组,然后不断寻找最小的边. 同样的题目:最小生成树 题 ...

  3. ACM第四站————最小生成树(普里姆算法)

    对于一个带权的无向连通图,其每个生成树所有边上的权值之和可能不同,我们把所有边上权值之和最小的生成树称为图的最小生成树. 普里姆算法是以其中某一顶点为起点,逐步寻找各个顶点上最小权值的边来构建最小生成 ...

  4. 第一次参加acm区域赛

    什么,这周天就要去参加acm焦作赛,简直不敢相信.从大一暑假七月份中旬到今天十一月23日,加入acm将近四个多月的时间,如今到了检验自己的时候了.aaaaaaaaaa.乌拉,必胜.打印个模板,在跑个步 ...

  5. The Parallel Challenge Ballgame[HDU1101]

    The Parallel Challenge Ballgame Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...

  6. ACM 整数划分(四)

    整数划分(四) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 暑假来了,hrdv 又要留学校在参加ACM集训了,集训的生活非常Happy(ps:你懂得),可是他最近 ...

  7. 2018.10.26 浪在ACM 集训队第四次测试赛

    2018.10.26 浪在ACM 集训队第四次测试赛 题目一览表 来源 考察知识点 完成时间 A 生活大爆炸版 石头剪刀布  NOIP 提高组 2014   模拟???  2018.11.9 B 联合 ...

  8. 2018牛客网暑期ACM多校训练营(第三场) A - PACM Team - [四维01背包][四约束01背包]

    题目链接:https://www.nowcoder.com/acm/contest/141/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

  9. 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)

    链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...

随机推荐

  1. 微信小程序车牌号码模拟键盘输入

    微信小程序车牌号码模拟键盘输入练习, 未经允许,禁止转载,抄袭,如需借鉴参考等,请附上该文章连接. 相关资料参考:https://blog.csdn.net/littlerboss/article/d ...

  2. php无限级分类----封装函数

    public function catetree($cateRes){//传递过来的数据资源 return $this->sort($cateRes); 调用函数 } public functi ...

  3. PHPExcel 导入Excel数据 (导出下一篇我们继续讲解)

    一:使用composer下载 phpoffice/phpexcel 或者直接下载安装包 composer require phpoffice/phpexcel 二 1:导入数据 原理:读取文件,获取文 ...

  4. 基于OMAPL138的字符驱动_GPIO驱动AD9833(三)之中断申请IRQ

    基于OMAPL138的字符驱动_GPIO驱动AD9833(三)之中断申请IRQ 0. 导语 学习进入到了下一个阶段,还是以AD9833为例,这次学习是向设备申请中断,实现触发,在未来很多场景,比如做用 ...

  5. C++ STL lower_bound()和upper_bound()

    lower_bound()和upper_bound()用法 1.在数组上的用法 假设a是一个递增数组,n是数组长度,则 lower_bound(a, a+n, x):返回数组a[0]~a[n-1]中, ...

  6. Python入门 (三)

    迭代器与生成器 迭代器 迭代是Python最强大的功能之一,是访问集合元素的一种方式. 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器 ...

  7. JavaWeb——升级赛-学生成绩管理系统(2).java---19.01.03

    dao.java package Dao; import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLExcept ...

  8. 从官网下载centos

    今天想从官网下载6.5版本的CentOS,结果找了好一会儿才找到,赶紧记录下来,以备以后查询. 第一步在百度搜索centos,点击"Download CentOS",如下图所示. ...

  9. leetcode--笔记8 Fizz Buzz

    题目要求: Write a program that outputs the string representation of numbers from 1 to n. But for multipl ...

  10. 「日常训练&知识学习」莫队算法(二):树上莫队(Count on a tree II,SPOJ COT2)

    题意与分析 题意是这样的,给定一颗节点有权值的树,然后给若干个询问,每次询问让你找出一条链上有多少个不同权值. 写这题之前要参看我的三个blog:Codeforces Round #326 Div. ...