Poj_1511_invitation Cards (Shortest way)

Source: Internet
Author: User

Invitation Cards
Time Limit: 8000MS Memory Limit: 262144K
Total Submissions: 21615 Accepted: 7089

Description

In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia is aware of this fact. They want to propagate theater and, the most of all, antique comedies. They has 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 have assigned exactly one bus stop and he or she stays there the whole day and gives invitation to P Eople travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing a nd robbery.

The transport system is very special:all lines be unidirectional and connect exactly. 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 fee for transport between and stops are given by special tables and are payable on the spot. The lines is planned in such a to, that's round trip (i.e. a journey starting and finishing at the same stop) passes Through a central Checkpoint Stop (CCS) where each passenger have to pass a thorough check including body scan.

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

Input

The 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 the 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 is 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 is positive integers the sum of which is smaller than 1000000000. You can also assume it's always possible to get from any stop to any other stop.

Output

For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of it S 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


Test instructions: In a graph, 1 to the shortest point of all points and the sum of all points to 1 of the shortest.

Analysis: It is obvious that using SPFA or dijkstra+heap, the shortest path is calculated along the storage edge, then the side is reversed to the shortest, and then summed. If the problem with vector map, then it is easy to card time, such as the storage diagram I use two pro-table directly stored on the tle, and later changed to a pro-table before. However, this is still the card time, you can continue to optimize. Test instructions is very clear, the number of edges is 1000000, then we can directly use the structure of the array to save the side, so that the sub-operation on the array, it will not be time-consuming. See the code for details:

Title Link: http://poj.org/problem?id=1511

Code Listing:

Vector implementation (high complexity of time):

#include <queue> #include <vector> #include <stdio.h> #include <string.h> #include <iostream > #include <algorithm>using namespace std;typedef long long ll;const int Maxn=1000000+5;const int MAX_DIS=1E9 + 5;    struct edge{int to,dis;        Edge (int to,int dis) {this--to =;    this, dis = dis; }};struct edge{int From,to,dis;} G[maxn];int t;int n,q;int a,b,c;int d[maxn];bool vis[maxn];typedef PAIR&LT;INT,INT&GT;P;VECTOR&LT;EDGE&GT;GRAPH[MAXN    ];void Clear () {for (int i=1;i<=n;i++) {graph[i].clear ();    }}void input () {scanf ("%d%d", &n,&q);    Clear ();        for (int i=0;i<q;i++) {scanf ("%d%d%d", &g[i].from,&g[i].to,&g[i].dis);    Graph[g[i].from].push_back (Edge (G[i].to,g[i].dis));        }}ll Dijkstra () {fill (D+1,d+1+n,max_dis);    Priority_queue<p,vector<p>,greater<p> >q;    while (Q.size ()) Q.pop ();    d[1]=0;    Q.push (P (0,1)); while (Q.size ()) {P p=q.top (); Q.poP ();        int V=p.second;        if (D[v]<p.first) continue;            for (int i=0;i<graph[v].size (); i++) {edge& e=graph[v][i];                if (D[e.to]>d[v]+e.dis) {D[e.to]=d[v]+e.dis;            Q.push (P (d[e.to],e.to));    }}} ll ret=0;    for (int i=1;i<=n;i++) ret+=d[i]; return ret;}    ll SPFA () {memset (vis,false,sizeof (VIS));    Fill (D+1,d+1+n,max_dis);    queue<int>q;    while (!q.empty ()) Q.pop ();    d[1]=0;    Vis[1]=true;    Q.push (1);        while (!q.empty ()) {int P=q.front (); Q.pop ();        vis[p]=0;            for (int i=0;i<graph[p].size (); i++) {edge& e=graph[p][i];                if (D[e.to]>d[p]+e.dis) {D[e.to]=d[p]+e.dis;                    if (!vis[e.to]) {vis[e.to]=true;                Q.push (e.to);    }}}} ll ret=0;    for (int i=1;i<=n;i++) ret+=d[i]; return ret;} void Dijkstra_solve () {ll ans=dijkstra ();    Clear ();    for (int i=0;i<q;i++) {graph[g[i].to].push_back (Edge (G[i].from,g[i].dis));    } Ans+=dijkstra (); printf ("%i64d\n", ans);}    void Spfa_solve () {ll ans=spfa ();    Clear ();    for (int i=0;i<q;i++) {graph[g[i].to].push_back (Edge (G[i].from,g[i].dis));    } ANS+=SPFA (); printf ("%i64d\n", ans);}    int main () {scanf ("%d", &t);        while (t--) {input ();        Dijkstra_solve ();    Spfa_solve (); }return 0;}

struct array implementation (less time complexity):

#include <queue> #include <vector> #include <stdio.h> #include <string.h> #include <iostream > #include <algorithm>using namespace std;typedef long long ll;const int Maxn=1000000+5;const int MAX_DIS=1E9 + 5; struct Edge{int to,dis,next;} Graph[maxn];struct edge{int From,to,dis;} G[maxn];int t;int n,q;int num;int d[maxn];bool vis[maxn];int head[maxn];typedef pair<int,int>p;void Clear () {num=    0;    memset (head,-1,sizeof (head)); memset (graph,0,sizeof (graph));}    void Add (int u,int v,int dis) {graph[num].to=v;    Graph[num].dis=dis;    Graph[num].next=head[u]; head[u]=num++;}    void input () {scanf ("%d%d", &n,&q);    Clear ();        for (int i=0;i<q;i++) {scanf ("%d%d%d", &g[i].from,&g[i].to,&g[i].dis);    Add (G[i].from,g[i].to,g[i].dis);    }}ll Dijkstra () {fill (D+1,d+1+n,max_dis);    Priority_queue<p,vector<p>,greater<p> >q;    while (Q.size ()) Q.pop ();    d[1]=0;    Q.push (P (0,1));     while (Q.size ()) {   P P=q.top ();        Q.pop ();        int V=p.second;        if (D[v]<p.first) continue; for (int k=head[v];k>-1;k=graph[k].next) {if (D[graph[k].to]>d[v]+graph[k].dis) {d[graph[k].                To]=d[v]+graph[k].dis;            Q.push (P (d[graph[k].to],graph[k].to));    }}} ll ret=0;    for (int i=1;i<=n;i++) ret+=d[i]; return ret;}    ll SPFA () {memset (vis,false,sizeof (VIS));    Fill (D+1,d+1+n,max_dis);    queue<int>q;    while (!q.empty ()) Q.pop ();    d[1]=0;    Vis[1]=true;    Q.push (1);        while (!q.empty ()) {int V=q.front (); Q.pop ();        vis[v]=0; for (int k=head[v];k>-1;k=graph[k].next) {if (D[graph[k].to]>d[v]+graph[k].dis) {d[graph[k].                To]=d[v]+graph[k].dis;                    if (!vis[graph[k].to]) {vis[graph[k].to]=true;                Q.push (graph[k].to);    }}}} ll ret=0;    for (int i=1;i<=n;i++) ret+=d[i]; Return RET;}    void Dijkstra_solve () {ll ans=dijkstra ();    Clear ();    for (int i=0;i<q;i++) {Add (G[i].to,g[i].from,g[i].dis);    } Ans+=dijkstra (); printf ("%i64d\n", ans);}    void Spfa_solve () {ll ans=spfa ();    Clear ();    for (int i=0;i<q;i++) {Add (G[i].to,g[i].from,g[i].dis);    } ANS+=SPFA (); printf ("%i64d\n", ans);}    int main () {scanf ("%d", &t);        while (t--) {input ();        Dijkstra_solve ();    Spfa_solve (); }return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Poj_1511_invitation Cards (Shortest way)

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.