最短路spfaPOJInvitation Cardsj解题报告

来源:互联网 发布:redial 软件 编辑:程序博客网 时间:2024/06/04 20:03

Invitation Cards
Time Limit: 8000MS Memory Limit: 262144K
Total Submissions: 22603 Accepted: 7398
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
题目大意:
给一个N个点的图,求1点到其他每个点最短路权值之和sum1,然后再求反向最短路(其他所有点到1点最短距离)之和sum2。输出sum1+sum2
因为vector的适合经常修改,本身是链表结构,每次插入数据都会消耗时间来申请内存并和前面建立联系,虽然可以支持下标访问,但是效率肯定赶不上数组的下标访问,所以有时候用vector会超时,这里我们用邻接表来存。
解题思路:
这题,求一次正向图最短路径,再求一次反向图最短路径(将返回的路径反转,也就是将回去的路转化成反图中去的路)。这样两次spfa,两次求和源点到每个点的距离和。
总结就是:
最短路题目,最好都使用邻接表+SPFA。这样,差不多就能解决了。除非特别出数据专门卡SPFA!

#include<cstring>#include<iostream>#include<queue>#include<cstdio>#define MAX 1000005#define inf 1000000000using namespace std;int InStopsNum,InEdgeNum;//输入的P和Qint dist[MAX];//源点到每个点的距离int Adjacentedge[MAX];//每个点最新的邻边int startVertex[MAX],endVertex[MAX],weightVertex[MAX];//输入数据int vis[MAX];//判断点是否在队列里int edgeNum;long long ans;struct Edge{    int endV,priorEdge,weight;}edge[MAX];//只需记录每边的终点,权值和同一起点的编号比自己小的前一个邻边void addEdge(int startV,int endV,int weight){    edge[edgeNum].endV=endV;    edge[edgeNum].weight=weight;    edge[edgeNum].priorEdge=Adjacentedge[startV];    Adjacentedge[startV]=edgeNum++;}void init(){    edgeNum=1;    memset(Adjacentedge,-1,sizeof(Adjacentedge));    memset(vis,0,sizeof(vis));    fill(dist,dist+InStopsNum+1,inf);    dist[1]=0;}void spfa(){    queue<int>q;    q.push(1);    vis[1]=1;    while(!q.empty()){        int frontVertex=q.front();q.pop();        for(int i=Adjacentedge[frontVertex];i!=-1;i=edge[i].priorEdge){//遍历点frontVertex的所有邻边            if(dist[edge[i].endV]>dist[frontVertex]+edge[i].weight){//该边可以松弛                dist[edge[i].endV]=dist[frontVertex]+edge[i].weight;//松弛                if(!vis[edge[i].endV]) {//如果邻边的终点不在队列中,入队列                        vis[edge[i].endV]=1;                        q.push(edge[i].endV);                }            }        }    }    for(int i=2;i<=InStopsNum;i++)ans+=dist[i];}int main(){    freopen("input.txt","r",stdin);    freopen("output.txt","w",stdout);    int cases;    scanf("%d",&cases);    while(cases--){        ans=0;        scanf("%d%d",&InStopsNum,&InEdgeNum);        init();        for(int i=1;i<=InEdgeNum;i++){            scanf("%d%d%d",&startVertex[i],&endVertex[i],&weightVertex[i]);            addEdge(startVertex[i],endVertex[i],weightVertex[i]);//建立正向图        }        spfa();        init();        for(int i=1;i<=InEdgeNum;i++)            addEdge(endVertex[i],startVertex[i],weightVertex[i]);//建立反向图        spfa();        printf("%lld\n",ans);    }    return 0;}
0 0
原创粉丝点击