UVa 10986:Sending email (Dijkstra优化, SPFA)

链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1927

题目:

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

3
2 1 0 1
0 1 100
3 3 2 0
0 1 100
0 2 200
1 2 50
2 0 0 1

Sample Output
Case #1: 100
Case #2: 150
Case #3: unreachable

Problemsetter: Igor Naverniouk

题目大意:

给一个图, 求从s点到t点的最小距离。

分析与总结:

赤裸裸的最短路,但n太大显然是不能用邻接矩阵的,需要用邻接表+优先队列优化。

代码:

1. Dijkstra,  0.148s

#include<cstdio>
#include<cstring>
#include<utility>
#include<queue>
using namespace std;  

typedef pair<int,int>pii;  

priority_queue<pii,vector<pii>,greater<pii> >q;  

const int N = 100005;
const int INF = 1000000000;  

int n, m, beg, end, k;
int head[N], next[N], u[N], v[N], w[N], d[N];
bool vis[N];  

inline void read_graph(){
    scanf("%d%d%d%d",&n,&m,&beg,&end);
    memset(head, -1, sizeof(head));
    for(int e=1; e<=m; ++e){
        scanf("%d%d%d",&u[e],&v[e],&w[e]);
        u[e+m]=v[e], v[e+m]=u[e], w[e+m]=w[e];
        next[e] = head[u[e]];
        head[u[e]] = e;
        next[e+m] = head[u[e+m]];
        head[u[e+m]] = e+m;
    }
}  

inline void Dijkstra(int src){
    memset(vis, 0, sizeof(vis));
    for(int i=0; i<n; ++i) d[i] = INF;
    d[src] = 0;
    q.push(make_pair(d[src], src));
    while(!q.empty()){
        pii u = q.top();  q.pop();
        int x = u.second;
        if(vis[x]) continue;
        vis[x] = true;
        for(int e=head[x]; e!=-1; e=next[e])if(d[v[e]] > d[x]+w[e]){
            d[v[e]] = d[x]+w[e];
            q.push(make_pair(d[v[e]], v[e]));
        }
    }
}  

int main(){
    int T,cas=1;
    scanf("%d",&T);
    while(T--){
        read_graph();
        Dijkstra(beg);
        printf("Case #%d: ",cas++);
        if(d[end]!=INF) printf("%d\n", d[end]);
        else puts("unreachable");
    }
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索servers
, dijkstra
, message
, required
, email
, The
sending
spfa dijkstra、spfa和dijkstra、spfa优化、堆优化spfa、spfa的优化,以便于您获取更多的相关知识。

时间: 2024-10-03 18:22:01

UVa 10986:Sending email (Dijkstra优化, SPFA)的相关文章

uva 10986 - Sending email

点击打开链接uva 10986 1思路: SPFA + 无向图邻接表2分析:   1题目给定的n最大20000,m最大50000,分析复杂度后发现只有SPFA最靠谱.   2分析题目的样列可知,这一题是要用邻接矩阵来存储无向图,所以要注意无向图怎么存储在邻阶表中   2连接表的横列有N项,纵列也是N项.形成的N*N项每项都被称为边结点,每项都有纵横两个坐标,例如点(N,N-1),表示的就是从第N点向第N-1点有无路径.由于有E条边,自然有E条路径,但是由于无向=双向,所以要乘以2 3代码: #i

Sending Email via ASP.NET and CDONTS[vs bate2 等级 中、高]

asp.net <%@ Page Language="VB" EnableSessionState="False" EnableViewState="False" Trace="False" Debug="False" Strict="True" %><%@ Import Namespace="System.Web.Mail" %> <s

UVa 340 Master-Mind Hints:优化查找&amp;amp;复制数组

340 - Master-Mind Hints Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=276 MasterMind is a game for two players. One of them, Designer, selects a secret

Sending Email via ASP.NET and CDONTS[vs bate2 等级 中

<%@ Page Language="VB" EnableSessionState="False" EnableViewState="False" Trace="False" Debug="False" Strict="True" %><%@ Import Namespace="System.Web.Mail" %> <script la

最短路专题【完结】

第一题 hdu 1317 XYZZY 点击打开hdu 1317 思路: 1 题目的图是一个有向图,并且可能存在环.第一个点的能量值为100,边的权值利用能量大小,例如2点为-60,如果1->2那么value[1][2] = -602 题目明确指出如果是要win的话,那么必须是经过的每条边都要大于0.那么我们只要把那些经过松弛操作后的点大于0的入队即可,小于等于0的点肯定不会出现在最终的路径上.3 如果存在正环的话,那么就有能量值无限大,那么这个时候只要判断这个点能否到达n4 判断是否是有环还是五

Linux下使用Evolution收发Email

1. 安装Evolution Evolution是一个整合了邮件,日历,计划任务,地址本功能的套件.以root身份运行apt-get install evolution就可以安装上Evolution了. 2. 设置Email账号 从任务栏的Application(程序)菜单中选择运行Evolution,然后从Evolution的Edit(编辑)菜单中选择Preferences(首选项)打开Evolution Settings设置窗口.点击的Mail Accounts,再点击Add按钮启动Evol

在Web上利用System.Web.Mail发送EMail

web 这是个vb.net的例子~ Email.aspx <%@ Page Language="vb" AutoEventWireup="false" Codebehind="Email.aspx.vb" Inherits="asif.SendEmail"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">&

如何用大数据优化技术提高LinkedIn内容运营效果数十倍

我将和大家一起从LinkedIn的战略开始,认识一下LinkedIn内容运营的历史地位和作用,分享如何运用大数据优化内容运营效果数十倍的成功经验. LinkedIn的战略并非从盈利入手 LinkedIn (领英) 由Paypal黑帮成员之一Reid Hoffman创建于2002年,致力于向全球职场人士提供沟通平台.作为全球最大的职业社交网站,目前LinkedIn会员人数在世界范围内已超过4亿.其中日活跃用户高达1亿. 很多公司都是以盈利为KPI,为一切产品设计.市场策略的唯一指挥棒.Linked

Send email in oracle

  1. CREATE PACKAGE   CREATE OR REPLACE PACKAGE demo_mail IS ----------------------- Customizable Section ----------------------- -- Customize the SMTP host, port and your domain name below.smtp_host VARCHAR2(256) := 'smtp.eygle.com';smtp_port PLS_IN