Invitation Cards - dijkstra的优化、转置(详解)

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

分析:

这道题着实做了好久啊……有几个坑

(1)邻接表会tle的,之前学dijkstra的优化时,不懂前向星,所以用邻接表,在这道题上,vector<int>G[N],也就是说,我设了1e^6个vector数组就stl了呗……

(2)可能会超过int啊,用long long

题意就是求1到各个点的最小price之和加上各个点到1的最小price之和,去时好求,回来时要把“矩阵”转置一下,就变成了别的点到1的最短路径


总结一下前向星吧:

核心代码:

int cnt=1,head[N];//head初始化为-1
struct proc{
    int to,cost,next;
}edge[N];

void add(int a,int b,int c){
    edge[cnt].to=b;
    edge[cnt].cost=c;
    edge[cnt].next=head[a];
    head[a]=cnt++;
}

edge[cnt].next指向同一起点的上一条边的编号

head[a]的值是最后一个输入的与结点a相连的编号(cnt表示编号)

如输入:

1 2 10
2 1 60
1 3 20
3 4 10
2 4 5
4 1 50

edge[1].next=-1;    edge[2].next=-1;    edge[3].next=1;   edge[4].next=-1;    edge[5].next=2;    edge[6].next=-1;

head[1]=1;             head[2]=2;             head[1]=3;          head[3]=3;             head[2]=5;           head[4]=6;

可看出用红色标注的,编号为3和编号为1的点是同一起点,编号为5和编号为2是同一起点

使用时(比如我们要遍历1连接的所有点):

for(int i=head[1];i!=-1;i=edge[i].next){
    v=edge[i].to;//表示1所连的点
}

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>

using  namespace std;
#define ll long long
typedef pair<ll,int> P;
const int N=1000005,INF=0x7f7f7f7f;

int head[N],n,m,cnt;
int from[N],to[N];
ll cost[N],d[N];

struct proc{
    int to,next;
    ll cost;
}edge[N];

void init(){
    cnt=1;
    memset(edge,0,sizeof(edge));
    memset(head,-1,sizeof(head));
}

void add(int a,int b,ll c){
    edge[cnt].to=b;
    edge[cnt].cost=c;
    edge[cnt].next=head[a];
    head[a]=cnt++;
}

void dijkstra(int s){
    for(int i=1;i<=n;i++){
        d[i]=INF;
    }
    d[s]=0;
    priority_queue<P,vector<P>,greater<P> >q;
    q.push(P(d[s],s));

    while(!q.empty()){
        P p=q.top();q.pop();
        int v=p.second;
        if(d[v]<p.first)continue;

        for(int i=head[v];i!=-1;i=edge[i].next){
            if(d[edge[i].to]>d[v]+edge[i].cost){
                d[edge[i].to]=d[v]+edge[i].cost;
                q.push(P(d[edge[i].to],edge[i].to));
            }
        }
    }
}

int main(){
    int cas,a,b,c;
    scanf("%d",&cas);

    while(cas--){
        ll ans=0;
        init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++){
            scanf("%d%d%lld",&from[i],&to[i],&cost[i]);
            add(from[i],to[i],cost[i]);
        }
        dijkstra(1);
        for(int i=1;i<=n;i++){
            ans+=d[i];
        }
        init();
        for(int i=1;i<=m;i++){
            add(to[i],from[i],cost[i]);
        }
        dijkstra(1);
        for(int i=1;i<=n;i++){
            ans+=d[i];
        }
        printf("%lld\n",ans);
    }

}

猜你喜欢

转载自blog.csdn.net/m0_37579232/article/details/80205801