HDU 1535 Invitation Cards: Multi-source point to single-point shortest

Source: Internet
Author: User
Tags time limit

Link:

http://acm.hdu.edu.cn/showproblem.php?pid=1535

Topic:

Invitation Cards
Time limit:10000/5000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 1044 accepted Submission (s): 459

Problem Description
In the "Age of" television, not many people attend theater performances. Antique comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, antique comedies. They have printed invitation cards with the necessary information and the programme. A lot of students were hired to distribute this invitations among. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives to P Eople travelling by bus. A special course was taken where students learned you to influence people and what is the difference between influencing a nd robbery.
The transport system is very special:all lines are unidirectional and connect exactly two. Buses leave the originating stop with passangers each half a hour. After reaching the "destination stop they return empty to the" originating stop, where they wait until "next full half" hour, e.g. x:00 or x:30, where ' X ' denotes the hour. The fee for transport between two stops are given by special tables and are payable on the spot. The lines are planned in such a way, which is all round trips (i.e. a journey starting and finishing at the same stop) passes Through a Checkpoint Stop (CCS) where each passenger has to pass a thorough check including the body scan.

All of the ACM student members leave the CCS each morning. Each volunteer to one predetermined the stop to invite passengers. There are as many volunteers as stops. At the ' end of the ', all students travel back to CCS. You are are to write a computer program which helps ACM to minimize the amount of $ to pay every day for the transport of T Heir employees.

Input
The input consists of N cases. The ' the ' input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= p,q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. Then There are Q lines, each describing one bus line. Each of the lines contains exactly three numbers-the stop, the originating stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it are always possible to get and any stop.

Output
For each case, print one line containing the minimum amount of $ to is paid each day by ACM for the travel costs of it S volunteers.

Sample Input

2
2 2 1 2 2 1, 4 6 1 2 2 1 1 3 3 4
ten
2
4 5 4 1 50

Sample Output

46 210
Source
Europe 1998

Recommend
LL

The main effect of the topic:

There are numbered 1~p site, there is Q bus route, bus route only from a starting point directly to the terminal station, is one-way, each route has its own fare.

There are p individuals starting from 1 in the morning, they want to reach every bus station, and then return to point 1 at night. Ask the sum of the minimum costs of all people to and fro.

Analysis and Summary:

By the way, you can use the Single-source shortest path algorithm to find out the minimum total cost.

But it's not easy to get back, it is to reach the specified point 1 minimum cost from each point. If you ask for the shortest circuit for each point, then you will definitely timeout.

At this time, according to reverse thinking, the reverse rebuild the diagram (that is, originally u-->v into V-->u), and then use the Single-source Shortest path algorithm to find 1 to all points of the shortest path can be.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <utility  
    
> Using namespace std;  
    
typedef pair<int,int>pii;  
const int VN = 1000005;  
const int EN = VN;  
    
const int INF=0X7FFFFFFF; struct Edge{int V, Next, W;}  
E[en];  
    
Priority_queue<pii,vector<pii>,greater<pii> >q;  
int n,size;  
int HEAD[VN];  
int D[VN];  
    
int U[VN],V[VN],W[VN];  
    void Init () {size=0;  
    Memset (Head,-1, sizeof (head));  
while (!q.empty ()) Q.pop ();  
    } void Addedge (int u,int v,int W) {e[size].v=v, e[size].w=w;  
    E[size].next = Head[u];  
Head[u] = size++;  
    } void Dijkstra (int src) {for (int i=1; i<=n; ++i) D[i]=inf;  
    D[SRC] = 0;  
    Q.push (Make_pair (D[SRC],SRC));  while (!q.empty ()) {PII x=q.top ();  
        Q.pop ();  
        int u = x.second;  
        if (D[u]!=x.first) continue;  
            for (int e=head[u]; E!=-1 e=e[e].next) {int tmp = D[u] + e[e].w;  
                if (d[e[e].v]>tmp) {D[E[E].V] = tmp;  
            Q.push (Make_pair (TMP,E[E].V));  
    int main () {int t,m;  
    scanf ("%d", &t);  
        while (t--) {scanf ("%d%d", &n,&m);  
        Init ();  
            for (int i=0; i<m; ++i) {scanf ("%d%d%d", &u[i],&v[i],&w[i]);  
        Addedge (U[i],v[i],w[i]);  
        } Dijkstra (1);  
        int ans=0;  
        for (int i=1; i<=n; ++i) ans + = d[i];   
        Init ();  
        for (int i=0; i<m; ++i)//Reverse map Addedge (v[i],u[i],w[i));  
        Dijkstra (1);  
        for (int i=1; i<=n; ++i) ans + = d[i];  
    printf ("%d\n", ans);  
return 0; }

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.