POJ 1511 Invitation Cards

来源:互联网 发布:淘宝名字怎么改 编辑:程序博客网 时间:2024/05/27 14:14

Invitation Cards

Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 29752 Accepted: 9928

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

题意

​ 给出一个有向图,n的点,m条边,问从1节点出发到2,3,…n 这些节点路程和从从这些节点回来到节点1的路程和最小值。

解题思路

​ 把图正向存一遍,跑一遍dijkstra,求出1到各个点的距离和。然后再把图反向存一遍,跑一遍dijkstra,求出1到各个点的距离和。思路很清晰。

​ 开始的时候用的dijkstra+vector+heap,结果TLE了。然后把vector换成了邻接表,结果过了。看来STL虽然方便,但是速度太慢。注意数据很大,int会炸。

#include<stdio.h>#include<iostream>#include<stdlib.h>#include<queue>using namespace std;#define maxn 1000005#define inf 0x3f3f3f3ftypedef struct Edgenode{    int x,val;    Edgenode *next;} Edgenode;typedef struct Edgehead{    int x;    Edgenode *firstedge;} Edgehead,adjlist[maxn];typedef struct graph{    int n,m;    adjlist Adjlist;} graph;graph g;long long d[maxn];int flag;struct node{    int x,y,val;} nodes[maxn];void init(){    for(int i=0; i<=g.n; i++)        g.Adjlist[i].firstedge=NULL;}void build(){    int a,b,c;    Edgenode *p;    for(int i=0; i<g.m; i++)    {        if(flag)/*正向建图*/        {            p=(Edgenode *)malloc(sizeof(Edgenode));            p->x=nodes[i].y;            p->val=nodes[i].val;            p->next=g.Adjlist[nodes[i].x].firstedge;            g.Adjlist[nodes[i].x].firstedge=p;        }        else/*反向建图*/        {            p=(Edgenode *)malloc(sizeof(Edgenode));            p->x=nodes[i].x;            p->val=nodes[i].val;            p->next=g.Adjlist[nodes[i].y].firstedge;            g.Adjlist[nodes[i].y].firstedge=p;        }    }}struct cmp{    bool operator()(const int a,const int b)    {        return d[a]>d[b];    }};long long dijkstra(){    priority_queue<int,vector<int>,cmp> q;    for(int i=2; i<=g.n; i++)        d[i]=inf;    d[1]=0;    q.push(1);    while(!q.empty())    {        int now=q.top();        q.pop();        Edgenode *p=g.Adjlist[now].firstedge;        while(p)        {            if(d[p->x]>d[now]+p->val)            {                d[p->x]=d[now]+p->val;                q.push(p->x);            }            p=p->next;        }    }    long long ans=0;    for(int i=2; i<=g.n; i++)        ans+=d[i];    return ans;}int main(){    //freopen("in.txt","r",stdin);    int t;    cin>>t;    while(t--)    {        scanf("%d%d",&g.n,&g.m);        for(int i=0; i<g.m; i++)            scanf("%d%d%d",&nodes[i].x,&nodes[i].y,&nodes[i].val);        long long ans=0;        flag=1;        init();        build();        ans+=dijkstra();        flag=0;        init();        build();        ans+=dijkstra();        printf("%lld\n",ans);    }    return 0;}