poj 1511 Invitation Cards(spfa)

来源:互联网 发布:程序员试用期个人总结 编辑:程序博客网 时间:2024/05/01 03:29
Invitation Cards
Time Limit: 8000MS Memory Limit: 262144KTotal Submissions: 16148 Accepted: 5249

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,先在正图上求一次,再在反图上求一次。。
AC代码:
#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <algorithm>#include <queue>#include <cmath>#include <vector>#include <cstdlib>using namespace std;const int N=1000010;const int INF=1<<30;struct Adjvex{    int v,w;    int next;}edge[2*N];int head[N],num;int dis[N],u[N],v[N],w[N],vis[N];int n,m;void init(){    for(int i=1;i<=n;i++)    {        head[i]=-1;    }    num=0;}void add(int u,int v,int w){    edge[num].v=v;    edge[num].w=w;    edge[num].next=head[u];    head[u]=num++;}void spfa(){    queue<int>Q;    for(int i=1;i<=n;i++)    dis[i]=INF;    dis[1]=0;            memset(vis,false,sizeof(vis));    vis[1]=true;    Q.push(1);    while(!Q.empty())    {        int x=Q.front();        Q.pop();        vis[x]=false;        for(int i=head[x];i!=-1;i=edge[i].next)        {            if(dis[edge[i].v]>dis[x]+edge[i].w)            {                dis[edge[i].v]=dis[x]+edge[i].w;                if(!vis[edge[i].v])                {                    vis[edge[i].v]=true;                    Q.push(edge[i].v);                }            }        }    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        long long ans=0;        scanf("%d%d",&n,&m);        for(int i=1;i<=m;i++)            scanf("%d%d%d",&u[i],&v[i],&w[i]);        init();        for(int i=1;i<=m;i++)        add(u[i],v[i],w[i]);        spfa();        for(int i=1;i<=n;i++)        ans+=dis[i];        init();        for(int i=1;i<=m;i++)        add(v[i],u[i],w[i]);        spfa();        for(int i=1;i<=n;i++)        ans+=dis[i];        printf("%lld\n",ans);    }    return 0;}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 户口转外省社保怎么办 户口迁出后医保怎么办 户口迁移行驶证怎么办 身份证改了驾照怎么办 迁户口后身份证怎么办 户口迁出后驾照怎么办 在异地学驾照怎么办 户口迁移后 驾照怎么办 无驾照开车违章怎么办 身份证地址错了怎么办 学生迁户口学籍怎么办 买新车临时牌怎么办 户口迁移换驾照怎么办 户口迁了身份证怎么办 户口迁移了医保怎么办 房屋卖了户口怎么办 驾照过期注销了怎么办 负全责不赔偿怎么办 青岛驾驶证过期了怎么办 驾驶证过了年检怎么办 驾驶证审证逾期怎么办 d驾驶证3年没捡怎么办 c1驾驶证3年没审怎么办 驾驶证换证外地怎么办 考驾驶证快到期怎么办 驾照到期人在国外怎么办 驾驶证3年没审怎么办c3 驾驶证几年没审怎么办 驾照体检过期了怎么办 b2驾照超过年检怎么办 驾驶证过审一年怎么办 驾照一年未年审怎么办 b驾照年审过期怎么办 摩托车驾驶证过期一年怎么办 驾驶证过期一年半怎么办 驾照过期了几天怎么办 驾照过期超过一年怎么办 考试驾照过期了怎么办 驾校考试过期了怎么办 驾驶证明过期了怎么办 驾驶证年过期了怎么办