HDU 1535 Invitation Cards(多源点到单点最短路)

来源:互联网 发布:红妍肌活精华露 知乎 编辑:程序博客网 时间:2024/05/21 10:48

链接:

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


题目:


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 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 travelling 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 fee 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 many 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. 

 

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 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. 
 

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

Source
Central Europe 1998
 

Recommend
LL
 


题目大意:

有编号1~P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接到达终点站,是单向的,每条路线有它自己的车费。

有P个人早上从1出发,他们要到达每一个公交站点, 然后到了晚上再返回点1。 求所有人来回的最小费用之和。


分析与总结:

依题意,去的时候用单源最短路算法便可求出去时的最小总花费。

但是回来的时候不好办,是从各个点到达指定的点1最小花费。如果对于每个点求一次最短路那么肯定会超时。

这时候,根据逆向思维,反向的再重新建立图(即原先是u-->v 变成v-->u),再用单源最短路算法求出1到所有点的最短路即可。



代码:

#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) // 反向建图            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;}


——  生命的意义,在于赋予它意义。

          
     原创 http://blog.csdn.net/shuangde800 , By   D_Double  (转载请标明)



原创粉丝点击