POJ 1511 Invitation Cards【来回最短路+SPFA】

来源:互联网 发布:网络二层 三层区别 编辑:程序博客网 时间:2024/05/16 09:35

Invitation Cards

Time Limit: 8000MS

 

Memory Limit: 262144K

Total Submissions: 24357

 

Accepted: 8049

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

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

Source

Central Europe 1998

 

题目大意:求一个来回最短路


思路:正向、反向两次建图两次SPFA即可。


注意数据比较大,要使用long long。


AC代码:

#include<stdio.h>#include<string.h>#include<queue>using namespace std;#define INF 10000000000LL#define ll long long intint head2[1000005];int head[1000005];struct EdgeNode{    int from;    int to;    int w;    int next;}e[1000005],ee[1000005];ll dis[1000005];int vis[1000005];int n,m,cont,cont2;void add(int from,int to,int w){    e[cont].from=from;    e[cont].to=to;    e[cont].w=w;    e[cont].next=head[from];    head[from]=cont++;}void add2(int from,int to,int w){    ee[cont2].from=from;    ee[cont2].to=to;    ee[cont2].w=w;    ee[cont2].next=head2[from];    head2[from]=cont2++;}void SPFA(){    memset(vis,0,sizeof(vis));    for(int i=1;i<=n;i++)dis[i]=INF;    dis[1]=0;    vis[1]=1;    queue<int >s;    s.push(1);    while(!s.empty())    {        int u=s.front();        s.pop();vis[u]=0;        for(int k=head[u];k!=-1;k=e[k].next)        {            int v=e[k].to;            int w=e[k].w;            if(dis[v]>dis[u]+w)            {                dis[v]=dis[u]+w;                if(vis[v]==0)                {                    vis[v]=1;                    s.push(v);                }            }        }    }}void SPFA2(){    memset(vis,0,sizeof(vis));    for(int i=1;i<=n;i++)dis[i]=INF;    dis[1]=0;    vis[1]=1;    queue<int >s;    s.push(1);    while(!s.empty())    {        int u=s.front();        s.pop();vis[u]=0;        for(int k=head2[u];k!=-1;k=ee[k].next)        {            int v=ee[k].to;            int w=ee[k].w;            if(dis[v]>dis[u]+w)            {                dis[v]=dis[u]+w;                if(vis[v]==0)                {                    vis[v]=1;                    s.push(v);                }            }        }    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        cont=0;        cont2=0;        memset(head,-1,sizeof(head));        memset(head2,-1,sizeof(head2));        for(int i=0;i<m;i++)        {            int x,y,w;            scanf("%d%d%d",&x,&y,&w);            add(x,y,w);            add2(y,x,w);        }        SPFA();        ll ans=0;        for(int i=1;i<=n;i++)ans+=dis[i];        SPFA2();        for(int i=1;i<=n;i++)ans+=dis[i];        printf("%lld\n",ans);    }}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小三把房子过户怎么办 小三把房子卖了怎么办 打印机ip地址变了怎么办 电脑ip地址错误不能上网怎么办 修改了注册表电脑无法启动怎么办 香水喷到衣服上有印怎么办 家里一股猫的味道怎么办 干菊花里面有虫怎么办 安装时显示程序已关闭怎么办 电脑一直重启开不了机怎么办 应用安装在sd卡打不开怎么办 安装ps打不开安装包怎么办 安装好的软件打不开怎么办? win10系统语言修改不了怎么办 一个月婴儿吵夜怎么办 玩游戏一直闪退怎么办 钱站一直闪退怎么办 win7重装连不上网怎么办 笔记本屏幕横过来了怎么办 3D贴图丢了怎么办 百度文库安装后手机打不开怎么办 win7系统不带usb驱动怎么办 手机网页上的pdf打不开怎么办 网页下载pdf后缀是.do怎么办 ps界面太小怎么办win10 ps软件打不开程序错误怎么办 ps打开后 未响应怎么办 ps图层无法解锁怎么办 ie8浏览器电脑不能用怎么办 系统要ie6.0才能打开怎么办 2g手机内存不够怎么办 2g运行内存不够怎么办 手机运行内存2g不够怎么办 手机无法加载程序秒退怎么办 电脑账户密码忘记了怎么办 玩绝地求生卡顿怎么办 地下城总运行时间错误怎么办 逆战更新太慢怎么办 win7我的电脑没了怎么办 剑灵启动游戏慢怎么办 网页页面结束进程也关不掉怎么办