UVA10986 - Sending email(Dijkstra)

题目链接

题目大意:给n个点,m条边,还有起点和终点,问起点到终点的最短距离,不可达unreachable。

解题思路:最短路问题,dijkstra算法。

代码:

#include <cstdio>
#include <queue>
#include <vector>
#include <string.h>  

using namespace std;
using std::make_pair;

typedef pair<int, int> pii;
priority_queue<pii, vector<pii>, greater<pii> >q;

const int INF = 0x3f3f3f3f;
const int maxn = 2e4;
const int maxm = 1e5 + 5;

int first[maxn];
int u[maxm], v[maxm], w[maxm], next[maxm];
int d[maxn];

void read_Graph (int n, int m) {

    for (int i = 0; i < n; i++)
        first[i] = -1;

    m *= 2;
    for (int i = 0; i < m; i++) {
        scanf ("%d%d%d", &u[i], &v[i], &w[i]);
        next[i] = first[u[i]];
        first[u[i]] = i;

        i++;
        u[i] = v[i - 1];
        v[i] = u[i - 1];
        w[i] = w[i - 1];
        next[i] = first[u[i]];
        first[u[i]] = i;
    }
}

int Dijkstra (int s, int t, int n) {

    for (int i = 0; i < n; i++)
        d[i] = (i == s) ? 0 : INF;

    q.push(make_pair(d[s],s));

    pii cur;
    while (!q.empty()) {

        cur = q.top();
        q.pop();

        if (cur.first != d[cur.second]) continue;

        for (int i = first[cur.second]; i != -1; i = next[i])
            if (d[v[i]] > d[u[i]] + w[i]) {
                d[v[i]] = d[u[i]] + w[i];
                q.push(make_pair(d[v[i]], v[i]));
            }
    }

    return d[t];
}

int main () {

    int T;
    scanf ("%d", &T);

    int n, m, s, t, cas = 0;
    while (T--) {

        scanf ("%d%d%d%d", &n, &m, &s, &t);

        read_Graph(n, m);
        int ans = Dijkstra(s, t, n);

        if (ans == INF)
            printf ("Case #%d: unreachable\n", ++cas);
        else
            printf ("Case #%d: %d\n", ++cas, ans);
    }
    return 0;
}
时间: 2024-10-11 06:28:17

UVA10986 - Sending email(Dijkstra)的相关文章

[题解]UVA10986 Sending email

链接:http://vjudge.net/problem/viewProblem.action?id=24941 描述:n个点,m条边的无向图,寻找从S到T的最短路. 思路:基础的单源点最短路 用Dijkstra或spfa都可以解决 这是我的实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6

uva 10986 Sending email (dijkstra)

uva 10986 Sending email "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 c

UVA 10986 Sending email(SPFA)

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

Sending e-mail with Spring MVC--转载

原文地址:http://www.codejava.net/frameworks/spring/sending-e-mail-with-spring-mvc Table of contents: 1.Spring framework’s support for e-mail 2.Required jar files 3.Creating e-mail sending form 4.Configuring SMTP server settings and Spring MVC 5.Creating

check-mk is not sending email

http://ubuntuforums.org/showthread.php?t=1971360 The problem is the DNS is not resolving (out off the domain) changed the domain to localhost and use smtp relay to recover sending email notifications.

Sending e-mail with Spring MVC---reference

reference from:http://www.codejava.net/frameworks/spring/sending-e-mail-with-spring-mvc Table of contents: 1.Spring framework’s support for e-mail 2.Required jar files 3.Creating e-mail sending form 4.Configuring SMTP server settings and Spring MVC 5

UVA 10986 Sending email 【dijkstra + 堆优化】

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1927 题意:n个点m条边,求s到e的最短距离 代码: #include<stdio.h> #include<iostream> #include<math.h> #include<stdlib.h> #include<cty

Spring – Sending E-Mail Via Gmail SMTP Server With MailSender--reference

Spring comes with a useful ‘org.springframework.mail.javamail.JavaMailSenderImpl‘ class to simplify the e-mail sending process via JavaMail API. Here’s a Maven build project to use Spring’s ‘JavaMailSenderImpl‘ to send an email via Gmail SMTP server.

UVA 10986 Sending email 最短路问题

基本的最短路问题 就是数据需要稍微处理一下.(N比较大)dijkstra也要优化.不优化应该会T: #include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <queue> #include <cctype> #i