HDU 1535 & amp; POJ 1511 Invitation Cards (SPFA template + reverse graph creation)

Source: Internet
Author: User

HDU 1535 & amp; POJ 1511 Invitation Cards (SPFA template + reverse graph creation)


Invitation Cards HDU: Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
POJ: Time Limit: 8000 MS Memory Limit: 262144 K
Problem Description In the age of television, not necessarily people attend theater CES. 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 all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people traveling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery.
The transport system is very special: all lines are unidirectional and connect exactly two stops. buses leave the originating stop with passangers each half an hour. after reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. x: 00 or X: 30, where 'x' denotes the hour. the parameter for transport between two stops is given by special tables and is payable on the spot. the lines are planned in such a way, that each round trip (I. e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan.

All the ACM student members leave the CCS each morning. each volunteer is to move to one predetermined stop to invite passengers. there are as follows volunteers as stops. at the end of the day, all students travel back to CCS. you are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees.


InputThe input consists of N cases. the first line of 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 originating stop, the destination 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 is always possible to get from any stop to any other stop.

OutputFor each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.

Sample Input
22 21 2 132 1 334 61 2 102 1 601 3 203 4 102 4 54 1 50

Sample Output
46210

SourceCentral Europe 1998


Question link: POJ: http://poj.org/problem? Id = 1511
HDU: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1535


A directed graph is used to calculate the Shortest Path weights from Point 1 to other points and the shortest path weights from other points to point 1 and


Problem Analysis: The HDU data is relatively short, and the time is short. The POJ data exceeds the int and wa data is sent a few times. The practice is to directly perform spfa at each point. In turn, we just need to build the image in turn, and try spfa again. A set of good boards is also very important. The dynamic distribution of boards in the book cannot afford to hurt. In fact, vector is quite time-consuming, I ran more than 4000 ms in poj, but it is easier to write.


POJ:

#include 
 
  #include 
  
   #include 
   
    #include 
    
     #define ll long longint const MAX = 1e6 + 5;ll const INF = 0xffffffff;using namespace std;struct NODE{    int v, w;    NODE(int vv, ll ww)    {        v = vv;        w = ww;    }};struct EDGE{    int u, v;    ll w;       }e[MAX];vector 
     
       vt[MAX];int p, q;ll ans, dist[MAX];bool vis[MAX];void SPFA(int v0){    memset(vis, false, sizeof(vis));    for(int i = 0; i <= p; i++)         dist[i] = INF;    dist[v0] = 0;    queue 
      
        Q; Q.push(v0); while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = false; int sz = vt[u].size(); for(int i = 0; i < sz; i++) { int v = vt[u][i].v; ll w = vt[u][i].w; if(dist[v] > dist[u] + w) { dist[v] = dist[u] + w; if(!vis[v]) { Q.push(v); vis[v] = true; } } } }}void Clear(){ for(int i = 1; i <= p; i++) vt[i].clear();}int main(){ int T; scanf("%d", &T); while(T--) { ans = 0; scanf("%d %d", &p, &q); for(int i = 0; i < q; i++) scanf("%d %d %lld", &e[i].u, &e[i].v, &e[i].w); Clear(); for(int i = 0; i < q; i++) vt[e[i].u].push_back(NODE(e[i].v, e[i].w)); SPFA(1); for(int i = 1; i <= p; i++) ans += dist[i]; Clear(); for(int i = 0; i < q; i++) vt[e[i].v].push_back(NODE(e[i].u, e[i].w)); SPFA(1); for(int i = 1; i <= p; i++) ans += dist[i]; printf("%lld\n", ans); }}
      
     
    
   
  
 

HDU:

#include 
 
  #include 
  
   #include 
   
    #include 
    
     int const MAX = 1e6 + 5;int const INF = 0xfffffff;using namespace std;struct NODE{    int v, w;    NODE(int vv, int ww)    {        v = vv;        w = ww;    }};struct EDGE{    int u, v, w;       }e[MAX];vector 
     
       vt[MAX];int p, q, ans, dist[MAX];bool vis[MAX];void SPFA(int v0)  {    memset(vis, false, sizeof(vis));    for(int i = 0; i <= p; i++)         dist[i] = INF;    dist[v0] = 0;    queue 
      
        Q; Q.push(v0); while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = false; int sz = vt[u].size(); for(int i = 0; i < sz; i++) { int v = vt[u][i].v; int w = vt[u][i].w; if(dist[v] > dist[u] + w) { dist[v] = dist[u] + w; if(!vis[v]) { Q.push(v); vis[v] = true; } } } }}void Clear(){ for(int i = 1; i <= p; i++) vt[i].clear();}int main(){ int T; scanf("%d", &T); while(T--) { ans = 0; scanf("%d %d", &p, &q); for(int i = 0; i < q; i++) scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].w); Clear(); for(int i = 0; i < q; i++) vt[e[i].u].push_back(NODE(e[i].v, e[i].w)); SPFA(1); for(int i = 1; i <= p; i++) ans += dist[i]; Clear(); for(int i = 0; i < q; i++) vt[e[i].v].push_back(NODE(e[i].u, e[i].w)); SPFA(1); for(int i = 1; i <= p; i++) ans += dist[i]; printf("%d\n", ans); }}
      
     
    
   
  
 


Related Article

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.