UVa 10986:sending Email (dijkstra optimization, SPFA)

Source: Internet
Author: User
Tags cas printf time limit

Link:

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

Topic:

Problem E
Sending email
Time Limit:3 seconds

"A new internet watchdog is creating a stir in
Springfield. Mr. X, if, is he 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 m Essage. What is the shortest time required to send a message from server S to server T along a sequence of cables? Assume that there are no delay incurred at any of the servers.

Input
The "a" of input gives the number of cases, N. n test Cases follow. Each one is 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'll each contain 3 integers:2 different servers (in the range [0, n-1]) this are connected by a Bidirec tional 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 T. Print "Unreachable" if there is no route the 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

The main effect of the topic:

For a graph, find the minimum distance from the s point to the T point.

Analysis and Summary:

Naked the shortest, but n too large is obviously not used adjacency matrix, need to use adjacency table + Priority queue optimization.

Code:

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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.