UVa10986_Sending email(最短)(白皮书图论的话题)
解决报告
思路:
裸裸的最短路。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define inf 0x3f3f3f3f
#define N 40000
#define M 100000
using namespace std;
struct node
{
int v,w,next;
}edge[M];
int head[N],dis[N],vis[N],cnt,n,m,s,t;
void add(int u,int v,int w)
{
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void spfa()
{
for(int i=0;i<n;i++)
{
dis[i]=inf;
vis[i]=0;
}
dis[s]=0;
vis[s]=1;
queue<int >Q;
Q.push(s);
while(!Q.empty()){
int u=Q.front();
Q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(dis[v]>dis[u]+edge[i].w)
{
dis[v]=dis[u]+edge[i].w;
if(!vis[v])
{
vis[v]=1;
Q.push(v);
}
}
}
}
}
int main()
{
int i,j,T,u,v,w,k=1;
scanf("%d",&T);
while(T--)
{
memset(head,-1,sizeof(head));
cnt=0;
scanf("%d%d%d%d",&n,&m,&s,&t);
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
spfa();
printf("Case #%d: ",k++);
if(dis[t]==inf)
printf("unreachable\n");
else printf("%d\n",dis[t]);
}
return 0;
}
Problem E
Sending email
Time Limit: 3 seconds
| "A new internet watchdog is creating a stir in Springfield. Mr. X, if that is his real name, has come up with a sensational scoop." |
Kent Brockman
There are n SMTP servers connected by network cables. Each of the m cables connects two computers and has a certain latency measured in milliseconds required to
send an email message. What is the shortest time required to send a message from server S to server T along a sequence of cables?
Assume that there is no delay incurred at any of the servers.
Input
The first line of input gives the number of cases, N. N test cases follow. Each one starts with a line containing n(2<=n<20000), m (0<=m<50000), S (0<=S<n)
and T (0<=T<n). S!=T. The next m lines will each contain 3 integers: 2 different servers (in the range [0, n-1]) that are connected by a
bidirectional cable and the latency, w, along this cable (0<=w<=10000).
Output
For each test case, output the line "Case #x:" followed by the number of milliseconds required to send a message from S toT. Print "unreachable" if there is no route from S to T.
| Sample Input | Sample Output |
3 |
Case #1: 100 |
Problemsetter: Igor Naverniouk
版权声明:本文博客原创文章。博客,未经同意,不得转载。
UVa10986_Sending email(最短)(白皮书图论的话题)的更多相关文章
- UVa-10986_Sending email (向前星+Dijkstra)
题意:给你点.边,求起点到终点的最短距离. 题解:由于题目的数据量特别大,所以需要用邻接表来存边,之后对Dijkstra算法稍微魔改一下就可以了,本来以为会超时,做好了打堆优化的准备,结果卡时间过了, ...
- 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树)
layout: post title: 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树) author: "luowentaoaa" ca ...
- <知识整理>2019清北学堂提高储备D5
今天主讲图论. 前言:图的定义:图G是一个有序二元组(V,E),其中V称为顶集(Vertices Set),E称为边集(Edges set),E与V不相交.它们亦可写成V(G)和E(G). 一.图的存 ...
- SDUT OJ 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)
数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss P ...
- 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)
分析:有向图里面找最短路径,原理就是每一步都走距离自己最近的路, 一旦发现走一步可以到,那么这个一定是最短的. #include <bits/stdc++.h> using namespa ...
- [图论]最短网络:prim
最短网络 目录 最短网络 Description Input Output Sample Input Sample Output 解析 代码 Description 农民约翰被选为他们镇的镇长!他其中 ...
- [图论]最短网络:kruskal
最短网络 目录 最短网络 Description Input Output Sample Input Sample Output 解析 代码 Description 农民约翰被选为他们镇的镇长!他其中 ...
- 黑客白皮书:如何成为一名黑客(附FAQ)
内容一览 为什么会有这份文档? 什么是黑客? 黑客应有的态度 黑客的基本技能 黑客文化中的地位 黑客和书呆子(Nerd)的联系 风格的意义 其它资源 FAQ(常问问题解答) 作为Jargon Fi ...
- FBI阅人术——用最短的时间了解一个人
FBI阅人术--用最短的时间了解一个人 和陌生人第一次见面时,要如何在一开始谈话的几分钟内,了解这个人?如何和对方拉近距离?如何找到对方喜爱的话题?如何让对方愿意开口? 这都得依靠细心而入微的观察力, ...
随机推荐
- Unity MVC框架 StrangeIoC
StrangeIoC是一个超轻量级和高度可扩展的控制反转(IoC)框架,专门为C#和Unity编写. 项目地址:https://github.com/strangeioc/strangeioc 文档地 ...
- SlidingMenu开源项目滑动界面的实现总结
先上图 须要准备的是先得在GitHub上下载ActionBarSherlock-master.zip,和SlidingMenu-master.zip这两个开源文件,然后解压这两个包,SlidingMe ...
- 经典回忆Effective C++ 1
c++ 联邦语言: typedef { unit C; unit Object-Oriented C++; unit Template C++; unit STL; }; notice: C++高效编 ...
- 利用h5标签在网页上播放音乐
方案1: <embed src="等一分钟.mp3" id="aa"> <input type=button value=暂停 onclick ...
- mysql表修改
CREATE TABLE tab2 AS (SELECT * FROM tab1)这种做法表的存储引擎也会采用服务器默认的存储引擎而不是源表的存储引擎,此种复制方法把表的内容也一起复制过来了. CRE ...
- hdu2175汉诺塔IX
汉诺塔IX Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- leetcode先刷_Binary Tree Level Order Traversal II
非常easy标题,在后面,我不认为它不是那么简单的回答更多的.我们将编写,没有人啊. 预购在基层上,加上节省每一层,加上从下往上的输出,是一家vector而一个stack那么问题,没有他,但另一方面- ...
- java实现代理domino web邮件下载
[背景] 近日在研究domino web邮件下载功能,下载的邮件能够导入foxmail.outlook邮件client,下载的邮件格式为eml. [參考代码](须要下载jmail.jar包) priv ...
- Prototype Pattern 原型模式
7.6 原型模式总结 原型模式作为一种快速创建大量相同或相似对象的方式,在软件开发中应用较为广泛,很多软件提供的复制(Ctrl + C)和粘贴(Ctrl + V)操作就是原型模式的典型应用,下面对该模 ...
- Linux在高铁项目的部署环境
因为Linux和Java像开源.所以,现在在server基本上使用部署Linux平台即server.然后部署项目.在开发项目的过程中.程序员绝大多数仍采用最经典windows操作系统,尽管Linux也 ...