POJ 1511 Invitation Cards(SPFA)

来源:互联网 发布:python redis 有效期 编辑:程序博客网 时间:2024/05/18 19:41
Invitation Cards
Time Limit: 8000MS Memory Limit: 262144KTotal Submissions: 25322 Accepted: 8383

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


题目大意:

    有一张图,问从节点1到各个点再回来的最短时间。


解题思路:

    建立正向图和反向图,直接用最短路即可。不过,第一次遇见卡用vector表示邻接表的题。这题必须用数组模拟链表,用规范的邻接表表示才可以。还有这题虽然题意说不会超过1e9不过我用int就无限WA,换成long long才过。。。


AC代码:

#include <iostream>#include <cstdio>#include <vector>#include <cstring>#include <queue>#include <vector>using namespace std;#define INF 0x3f3f3f3f3f3f3f3f#define mem(a,b) memset((a),(b),sizeof(a))#define LL long longstruct Edge{    int to;    LL cost;    int next;    Edge(int t=0,LL c=0,int n=0):to(t),cost(c),next(n){}};const int maxv=1000000+3;int V,M,num;Edge node[maxv*2];//数组模拟链表int G[maxv],rG[maxv];//正向图反向图bool vis[maxv];//在队列标志LL dist[maxv];//储存从起点到各个边的最短距离void SPFA(int start,int n,int G[])//起点,总共点数,使用哪个图{    for(int i=1;i<=n;++i)    {        vis[i]=false;        dist[i]=INF;    }    vis[start]=true;    dist[start]=0;    queue<int> que;    que.push(start);    while(!que.empty())    {        int u=que.front();        que.pop();        vis[u]=false;        for(int i=G[u];i!=-1;i=node[i].next)        {            int v=node[i].to;            if(dist[v]>dist[u]+node[i].cost)            {                dist[v]=dist[u]+node[i].cost;                if(!vis[v])                {                    vis[v]=true;                    que.push(v);                }            }        }    }}int main(){    int T;    scanf("%d",&T);    while(T--)    {        num=0;        scanf("%d%d",&V,&M);        for(int i=1;i<=V;++i)        {            G[i]=-1;            rG[i]=-1;        }        for(int i=0;i<M;++i)        {            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            node[num]=Edge(b,c,G[a]);            G[a]=num++;            node[num]=Edge(a,c,rG[b]);            rG[b]=num++;        }        LL ans=0;        SPFA(1,V,G);//去的花费        for(int i=2;i<=V;++i)            ans+=dist[i];        SPFA(1,V,rG);//回来的花费        for(int i=2;i<=V;++i)            ans+=dist[i];        printf("%lld\n",ans);    }        return 0;}


0 0
原创粉丝点击