HDU-1535-Invitation Cards(SPFA,邻接表)

来源:互联网 发布:哪个品牌的网络电视好 编辑:程序博客网 时间:2024/05/16 10:28

Invitation Cards

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3193 Accepted Submission(s): 1481

Problem 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

题意:第一行给出T,代表有T组数据。每组数据第一行给出P和Q,代表有P个站点Q条线路。接下来Q行,每行三个数xi,yi,c,代表从xi到yi花费为c。输出从1到各站点以及从各站点到1的最小花费和。

思路:考虑到P有一百万,无法建立邻接矩阵,只能用邻接表建图。然后SPFA一遍找到1到各点最小花费,累加后反向建图,再来一次SPFA找到各点到1最小花费,再次累加后输出即可。

综合性比较强的题目,尤其是反向建图时,加深了对邻接表的认识。

代码

#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<queue>using namespace std;const int maxn=1000005;//邻接表数组const int INF=99999999;//正无穷int u[maxn];int v[maxn];int w[maxn];int first[maxn];int nextn[maxn];//以上用于邻接表int dis[maxn];//存储1到各点或者各点到1的最短距离int vis[maxn];//已访问标记为1,初始化为0int P;//P个站点int Q;//Q条路线void init()//邻接表建图{    for(int i=0; i<=Q; i++) //初始化邻接表数组        first[i]=nextn[i]=-1;    for(int i=0; i<Q; i++)    {        scanf("%d%d%d",&u[i],&v[i],&w[i]);        nextn[i]=first[u[i]];        first[u[i]]=i;    }}void set_init()//邻接表反向建图{    for(int i=0; i<=Q; i++)        first[i]=nextn[i]=-1;    for(int i=0; i<Q; i++)    {        nextn[i]=first[v[i]];        first[v[i]]=i;    }}void SPFA(int point,int flag){//flag为1时,dis记录point到各点最短距离。//flag为0时,dis记录各点到point的最短距离  for(int i=0;i<=P;i++)  {      vis[i]=0;//初始化为未访问      dis[i]=INF;//point到各点距离初始化为正无穷  }  dis[point]=0;//自己到自己的距离为0  queue<int>q;//创建队列q  q.push(point);//起始结点入队  while(!q.empty())  {      point=q.front();//赋值给point只是为了减少变量      q.pop();      vis[point]=0;//先标记为未访问      for(int k=first[point];k!=-1;k=nextn[k])      {          int temp=(flag?v[k]:u[k]);//求point到各点或者各点到point          if(dis[temp]>dis[point]+w[k])//松弛各边          {              dis[temp]=dis[point]+w[k];              if(vis[temp]==0)//如果未访问              {                  vis[temp]=1;//标记为已访问                  q.push(temp);//结点入队              }          }      }  }}int main(){    int T;//T组数据    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&P,&Q);        init();//邻接表建图        SPFA(1,1);        int sum=0;//记录总路程        for(int i=2;i<=P;i++)            sum+=dis[i];        set_init();//反向建图        SPFA(1,0);        for(int i=2;i<=P;i++)            sum+=dis[i];        printf("%d\n",sum);    }}

网上看到还可以两次迪杰斯特拉解决,但是他们的代码粘上去全是CE!

0 0