Invitation Cards(dijkstra算法+堆优化)Poj 1151

题目:http://poj.org/problem?id=1511

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

题意:在电视时代,参加戏剧表演的人不多。古色古香的马利丁西亚喜剧家知道这一事实。他们想宣传戏剧,最重要的是,仿古喜剧。他们印制了所有必要资料和节目的邀请函。许多学生被雇来向人们分发这些请柬。每个学生志愿者都指定了一个公共汽车站,他或她在那里呆了一整天,并向乘坐公共汽车的人发出邀请。在这里,学生们学习了如何影响他人,以及影响和抢劫之间的区别。交通系统非常特殊:所有的线路都是单向的,正好连接两个车站。每半小时有一辆公共汽车载着乘客离开始发站。到达目的地停止后,它们返回空到起始停止,在那里等待到下一个完整的半小时,例如X:00或X:30,其中“X”表示小时。两站之间的交通费由特别表格提供,当场支付。这些路线的规划方式是,每一次往返(即在同一站开始和结束的旅程)都经过一个中央检查站(CCS),在那里每位乘客必须通过包括身体扫描在内的彻底检查。所有ACM学生成员每天早上都离开CCS。每个志愿者都要移动到一个预定的站来邀请乘客。志愿者和站的人数一样多。在一天结束时,所有的学生都回到CCS。你要写一个计算机程序,帮助ACM尽量减少每天支付给他们员工的交通费。

分析:由题目可以看出,本题就是求出从节点1到其他每个节点的最短路,在求出每个节点到1的最短路径,每个节点的最短路加和就可以。由于数据过大,使用dijkstra的堆优化。时间复杂度O(ElogE)。对于vector<Edge>E[MAXN]进行初始化后加边。最后直接套用模板就可以。不过因为,对于三个数组A[MAXN],B[MAXN],C[MAXN]应该放在主函数外作为全局变量,运行前分配空间,放在main里边是按照动态分配在堆里分配,空间可能不够就无法输入。

AC代码:(模板来自kuangbin)

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
#define INF 0x3f3f3f3f
#define MAXN 1000010
using namespace std;
struct qnode
{
    int v;
    int c;
    qnode (int _v=0,int _c=0):v(_v),c(_c){}
    bool operator <(const qnode &r)const{
            return c>r.c;
    }
};
struct Edge{
    int v,cost;
    Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
vector <Edge>E[MAXN];
bool vis[MAXN];
int dist[MAXN];
//点的编号从1开始
void Dijkstra(int n,int start)
{
    memset(vis,false,sizeof(vis));
    for(int i=1;i<=n;i++)
        dist[i]=INF;
    priority_queue<qnode>que;
    while(!que.empty())que.pop();
    dist[start]=0;
    que.push(qnode(start,0));
    qnode tmp;
    while(!que.empty())
    {
        tmp=que.top();
        que.pop();
        int u=tmp.v;
        if(vis[u])continue;
        vis[u]=true;
        for(int i=0;i<E[u].size();i++)
        {
            int v=E[tmp.v][i].v;
            int cost=E[u][i].cost;
            if(!vis[v]&&dist[v]>dist[u]+cost)
            {
                dist[v]=dist[u]+cost;
                que.push(qnode(v,dist[v]));
            }
        }
    }
}
void addedge(int u,int v,int w)
{
    E[u].push_back(Edge(v,w));
}
int A[MAXN];
int B[MAXN];
int C[MAXN];
int main()
{
    int N;
    int P,Q;

    scanf("%d",&N);
    while(N--)
    {
        scanf("%d%d",&P,&Q);
        for(int i=0;i<Q;i++)
        {
            scanf("%d%d%d",&A[i],&B[i],&C[i]);
        }
        for(int i=1;i<=P;i++)E[i].clear();
        for(int i=0;i<Q;i++)addedge(A[i],B[i],C[i]);
            Dijkstra(P,1);
        long long ans=0;
        for(int i=1;i<=P;i++)ans+=dist[i];
        for(int i=1;i<=P;i++) E[i].clear();
        for(int i=0;i<Q;i++)addedge(B[i],A[i],C[i]);
            Dijkstra(P,1);
        for(int i=1;i<=P;i++)ans+=dist[i];
        printf("%I64d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/guagua_de_xiaohai/article/details/81104387