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

链接:

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

2
2 2
1 2 13
2 1 33
4 6
1 2 10
2 1 60
1 3 20
3 4 10
2 4 5
4 1 50

Sample Output

46 210
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://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索int
, include
, ccs
, dsp ccs 报错
, dijkstra
, to
, and
, The
stop
hdu 1535、伊东红下马dv1535、虹梅路1535号、killer 1535、killer 1535 驱动,以便于您获取更多的相关知识。

时间: 2024-10-29 09:36:01

HDU 1535 Invitation Cards:多源点到单点最短路的相关文章

hdu 1535 Invitation Cards

点击打开链接hdu1535 思路:最短路+SPFA 分析: 1 题目要求的是总的最小的花费,意思就是要求每一个人的花费都最小. 2 由于每一个人都是从1出去,最后还是都要回到1的,那么求解的时候就要分成两部分"出去+回来":出去的话直接利用SPFA(1),1作为起点即可求出每一点的最小花费,回来的话如果是直接利用对每一个人进行SPFA,那么这样肯定超时.仔细想想要求的是每一个点到1的最小距离,那么由于给定的是一个有向图,那么只要重新建图把边反向,那么我们所求的问题就变成1到每一个点的最

poj 1511 Invitation Cards dijkstra+heap

     最近没有状态,不太难得一题,TLE了3次,WA了1次.      这题主要就是要正向,逆向两次dijkstra,因为稀疏图,所以用heap优化有明显作用.      注意会超出int范围,要用long long     代码太挫了,越写越挫,估计到瓶颈期了 /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */

HDU 1839 Delay Constrained Maximum Capacity Path(二分+最短路)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1839 题目: Delay Constrained Maximum Capacity Path Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 226    Accepted Submission(s): 98 Problem Descri

最短路专题【完结】

第一题 hdu 1317 XYZZY 点击打开hdu 1317 思路: 1 题目的图是一个有向图,并且可能存在环.第一个点的能量值为100,边的权值利用能量大小,例如2点为-60,如果1->2那么value[1][2] = -602 题目明确指出如果是要win的话,那么必须是经过的每条边都要大于0.那么我们只要把那些经过松弛操作后的点大于0的入队即可,小于等于0的点肯定不会出现在最终的路径上.3 如果存在正环的话,那么就有能量值无限大,那么这个时候只要判断这个点能否到达n4 判断是否是有环还是五

POJ 最短路问题题号汇总

求最短路基本的算法: 1>Dijkstra算法2>Bellman-Ford算法3>Floyd算法4>Floyd-Warshall算法5>Johnson算法6>A*算法 题目: 1.poj1062 昂贵的聘礼(中等)     此题是个经典题目:用Dijkstra即可:但是其中的等级处理需要一定的技巧:    要理解好那个等级制度:这个处理好,基本就是裸体Dijkstra: 2 poj1125 Stockbroker Grapevine(基本)    这个是简单Floyd,

HDU 1394 线段树单点更新求逆序数

题意:给出含有0 ~n-1 N个数组成的序列,有N次操作,每次把第一个数放到数列的最后,问这几次数列操作中最小的逆序数的值. 单点更新就可以,每一输入一个数,先查询有几个比这个数大的,再将这个值插入线段树中. #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define N 5005 struct node { i

HDU 2795 单点更新查询最大值

题意:给你一个h*w的广告板,每条通知的长度为1*wi,给出n个通知,如果可以放下尽量靠上放,对应的行里尽量靠左方,对于每条通知给出存放在哪条上. 这题最多n个节点,所以不用考虑h的最大范围.线段树中每点维护当前剩余宽度,查询最大剩余宽度是否满足,满足即查询靠上的节点并更新. #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace s

HDU 1166 线段树单点更新

第一行一个整数T,表示有T组数据.每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50).接下来每行有一条命令,命令有4种形式:(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人

hdu 1599 find the mincost route

点击打开链接hdu 1599 思路:最短路+floyd+最小环 分析: 1 题目要求的是能否有从某一个点出发至少经过两个不同点然后回到源点,有的话求最小路径长度. 2 题意很明确就是要求最小环问题 , 所以现在用到了floyd的一个扩展求解图上最小环. 3 <<floyd求解环中的最小环>>    1 为什么要在更新最短路之前求最小环:       在第k层循环,我们要找的是最大结点为k的环,而此时Dist数组存放的是k-1层循环结束时的经过k-1结点的最短路径,也就是说以上求出的