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阅人术--用最短的时间了解一个人 和陌生人第一次见面时,要如何在一开始谈话的几分钟内,了解这个人?如何和对方拉近距离?如何找到对方喜爱的话题?如何让对方愿意开口? 这都得依靠细心而入微的观察力, ...
随机推荐
- Java命令学习系列(7):Javap(转)
原文出处: Hollis(@Hollis_Chuang) javap是jdk自带的一个工具,可以对代码反编译,也可以查看java编译器生成的字节码. 一般情况下,很少有人使用javap对class文件 ...
- Linux下OpenCV的环境搭建(转)
OpenCV is the most popular and advanced code library for Computer Vision related applications today, ...
- Android 自己实现 NavigationView [Design Support Library(1)]
转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/46405409: 本文出自:[张鸿洋的博客] 一.概述 Google I/O 2 ...
- duilib底层机制剖析:窗口类与窗口句柄的关联
转载请说明原出处.谢谢~~ 看到群里朋友有人讨论WTL中的thunk技术,让我联想到了duilib的类似技术. 这些技术都是为了解决c++封装的窗口类与窗口句柄的关联问题. 这里是三篇关于thunk技 ...
- [Network]Application Layer
1 Principles of Network Applications 1.1 Application Architectures Client-Server Peer-to-Peer Hybird ...
- mongodb实现简单的增删改查
package mongoDB; import java.net.UnknownHostException; import java.util.ArrayList; import java.util. ...
- 完整的java字符串编码转换代码
package book.String; import java.io.UnsupportedEncodingException; /** *//** * 转换字符串的编码 * @author joe ...
- 【数据结构&&等差数列】KMP简介和算法的实现(c++ && java)
KMP算法假定了解案件的原则,其实很easy. KMP算法简述 关于根据自己的理解在这里. KMP该算法由三个发明人的名称(Knuth.Morris.Pratt)的首字母组成,又称字符串查找算法. 个 ...
- Linux date -s(转)
修改linux的时间可以使用date指令 修改日期: 时间设定成2009年5月10日的命令如下: #date -s 05/10/2009 修改时间: 将系统时间设定成上午10点18分0秒的命令如下. ...
- 最小路径覆盖 hdu 1151 hdu 3335
Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...