最短路(经典SPFA)-poj1511

来源:互联网 发布:韦德2006年总决赛数据 编辑:程序博客网 时间:2024/04/28 16:21
Invitation Cards
Time Limit: 8000MS Memory Limit: 262144KTotal Submissions: 16938 Accepted: 5501

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

首先要说的是,觉得这道题很经典,有必要认真做一下,第一次做spfa的题目,结果也是wa了很多次,找了半天没找出错,最后原来是初始化时,值附小了。

题意:有很多车站,每天很多辆车从1点出发,最后再回到1点,不同的路线有不同的费用,问最小费用是多少。

思路:我觉得这道题的难点是如何求回去的费用,方法是将所有的边再求从1到所有点的最短距离,还有个问题就是这个题的数据很大,我刚开始因为这个wa了很多遍,最后设成INT_MAX才过了。

#include<iostream>#include<cstring>#include<queue>#include<cstdio>#include<algorithm>#include<climits>using namespace std;const int maxn=1000000+10;const int maxl=INT_MAX;struct bus{    int v,next,sum;};bus acm[2][maxn];int p[2][maxn];long long dis[maxn];int l,q;long long sum;void spfa(int num){    int vis[maxn];    int start=1;    memset(vis,0,sizeof(vis));    for(int i=1;i<=l;i++)        dis[i]=maxl;    dis[start]=0;    vis[start]=1;    queue<int> stud;    stud.push(start);    while(!stud.empty())    {        int t=stud.front();        stud.pop();        for(int j=p[num][t]; j!=0; j=acm[num][j].next)        {            if(dis[acm[num][j].v]>acm[num][j].sum+dis[t])            {                dis[acm[num][j].v]=acm[num][j].sum+dis[t];                if(!vis[acm[num][j].v])                {                    vis[acm[num][j].v]=1;                    stud.push(acm[num][j].v);                }            }        }    }    for(int i=1; i<=l; i++)        sum+=dis[i];}int main(){    freopen("in.txt","r",stdin);    int n;    int from,to,cost;    scanf("%d",&n);    while(n--)    {        cin>>l>>q;        int st=1;        sum=0;        memset(p,0,sizeof(p));        for(int i=1; i<=q; i++)        {            scanf("%d%d%d",&from,&to,&cost);            acm[0][st].next=p[0][from];            acm[0][st].v=to;            acm[0][st].sum=cost;            acm[1][st].next=p[1][to];            acm[1][st].v=from;            acm[1][st].sum=cost;            p[0][from]=p[1][to]=st++;        }        spfa(0);        spfa(1);        cout<<sum<<endl;    }    return 0;}