POJ 1511 Invitation Cards-2016.12月计科院赛(最短路)

来源:互联网 发布:游戏视频录制软件 编辑:程序博客网 时间:2024/06/05 02:47
Invitation Cards
Time Limit: 8000MS Memory Limit: 262144KTotal Submissions: 26275 Accepted: 8735

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

S来回走两次,第一次很明显就是裸的DIJ,求从1到其他点的最短路,走了一半,另外一般就是要从其他点回到n,比赛的时候直接猜了一波反向建边,把图里面的单向边反方向建一次,再求一次dij不就是从其他点到起点的最短路么,猜对了居然。计科比赛数据比较小,n^n都可以过,POJ 上面就大一点,要用队列优化来做,6000+MS


#include<algorithm>#include<stdio.h>#include<queue>#include<string.h>#include<vector>using namespace std;#define INF 0x7fffffff#define maxn 1000005struct node{    int len;    int now;    bool operator <(const node &x)const    {        if(len>x.len)            return true;        else            return false;    }};priority_queue<node>que;int dis[maxn];int use[maxn];int M[maxn][3];vector<node>a[maxn];int n,m;void inti(){    for(int i=0;i<=n;i++)        dis[i]=INF;    for(int i=0;i<=n;i++)        a[i].clear();    memset(use,0,sizeof(use));    while(que.size())    {        que.pop();    }}int main(){    int x,y,z;    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        inti();        node temp,next;        for(int i=0;i<m;i++)        {            scanf("%d%d%d",&x,&y,&z);            M[i][0]=x;            M[i][1]=y;            M[i][2]=z;            temp.now=y;            temp.len=z;            a[x].push_back(temp);        }        for(int i=0;i<a[1].size();i++)        {            next.now=a[1][i].now;            next.len=a[1][i].len;                dis[a[1][i].now]=a[1][i].len;            que.push(next);        }        while(!que.empty())          {            temp=que.top();            que.pop();            if(use[temp.now]==1)                  continue;            use[temp.now]=1;            for(int i=0;i<a[temp.now].size();i++)            {                if(use[a[temp.now][i].now]==0&&dis[a[temp.now][i].now]>(dis[temp.now]+a[temp.now][i].len))                {                    dis[a[temp.now][i].now]=dis[temp.now]+a[temp.now][i].len;                     next.now=a[temp.now][i].now;                    next.len=dis[a[temp.now][i].now];                    que.push(next);                                        }            }        }        long long ans=0;        for(int i=2;i<=n;i++)            ans+=dis[i];        inti();        for(int i=0;i<m;i++)        {            y=M[i][0];            x=M[i][1];            z=M[i][2];            temp.now=y;            temp.len=z;            a[x].push_back(temp);        }        for(int i=0;i<a[1].size();i++)        {            next.now=a[1][i].now;            next.len=a[1][i].len;                dis[a[1][i].now]=a[1][i].len;            que.push(next);        }        while(!que.empty())          {            temp=que.top();            que.pop();            if(use[temp.now]==1)                   continue;            use[temp.now]=1;            for(int i=0;i<a[temp.now].size();i++)            {                if(use[a[temp.now][i].now]==0&&dis[a[temp.now][i].now]>(dis[temp.now]+a[temp.now][i].len))                {                    dis[a[temp.now][i].now]=dis[temp.now]+a[temp.now][i].len;                      next.now=a[temp.now][i].now;                    next.len=dis[a[temp.now][i].now];                    que.push(next);                                        }            }        }        for(int i=2;i<=n;i++)            ans+=dis[i];        printf("%lld\n",ans);    }return 0;}


0 0
原创粉丝点击