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. TinyMCE插件:RESPONSIVE filemanager 9 图片自动添加水印

    跟踪function() 搜索(filemanager/upload.php) 在代码中发现,上传成功后,会传回JSON信息数据,于是最后找到方法是 $upload_handler = new Upl ...

  2. opencv3 学习一 - Visual Studio 配置

    Step 1 下载最新版的Opencv3.4.2,见图片中的网址,选择 Win Pack. Step 2 安装Opencv3 到指定目录,见图片,路径后面会用到. Step 3 把安装目录下的bin路 ...

  3. spark----词频统计(一)

    利用Linux系统中安装的spark来统计: 1.选择目录,并创建一个存放文本的目录,将要处理的文本保存在该目录下以供查找操作: ① cd /usr/local ②mkdir mycode ③ cd ...

  4. WPF程序,运行时,结束时,要运行的操作(自动保存,检查单程序)

    /// <summary> /// App.xaml 的交互逻辑 /// </summary> public partial class App : Application { ...

  5. 【转】Odoo装饰器: one装饰

    one装饰器的作用是对每一条记录都执行对应的方法,相当于traditional-style中的function,无返回值! 应用举例: 定义的columns now = fields.Datetime ...

  6. 成都Uber优步司机奖励政策(2月28日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  7. 单目、双目和RGB-D视觉SLAM初始化比较

    无论单目.双目还是RGB-D,首先是将从摄像头或者数据集中读入的图像封装成Frame类型对象: 首先都需要将彩色图像处理成灰度图像,继而将图片封装成帧. (1) 单目 mCurrentFrame = ...

  8. 利尔达NB-IOT模组Coap数据AT+NMGS发送时返回-513的原因

    1. 利尔达NB-IOT模组使用AT+NMGS发送数据,返回-513的问题,大致有3种可能性,在硬件上,模组的射频电路分为A型和B型模组,所以烧写固件的时候,也要分为A和B型固件,如果烧写反了,那么R ...

  9. 这样的SQL居然能执行

    select /*! distinct   cities.id from cities  join countries on cities.id = countries.id limit 10 */;

  10. 「日常训练」Paths and Trees(Codeforces Round 301 Div.2 E)

    题意与分析 题意是这样的,定义一个从某点出发的所有最短路方案中,选择边权和最小的最短路方案,称为最短生成树. 现在求一棵最短生成树,输出总边权和与选取边的编号. 我们首先要明白这样一个结论:对一个图求 ...