POJ-1511 (Dijkstra + priority queue optimization)

Invitation Cards

POJ-1511

  • From this question I found a lot of problems, starting with fast input and output, ios :: --- where the line must be placed on the first line of the main function, which is the beginning of the previous input, otherwise the system crazy reported WA.
  • The second is, ios nothing wrong location after they frantically reported TLE, is timed out, the problem is the complexity of the algorithm or else, there is the input and output is not fast enough, so first of all eliminate the problem input and output, so I put ios scanf changed so this question before.
  • Facts have proved that, ios method is still not as fast as scanf, so the future still use scanf.
  • The second is the problem with this algorithm itself, this is already faster than n * n algorithm more, since the amount of data in this question too, this version is also very dangerous just before.
  • Thinking about this question is mainly positive, forward to go again, then go backwards again, and finally add up on it.
  • Recommend more than one method of the blog: https://blog.csdn.net/qq_39665840/article/details/81437812
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
int p,q;//p-stops;q-lines
struct edge{
    int to;
    int cost;
    edge(){}
    edge(int a,int b):to(a),cost(b){}
};
struct node{
    int dis;
    int to;
    node(){}
    node(int a,int b):dis(a),to(b){}
    bool operator<(const node& t)const{
        return dis>t.dis;
    }
};
vector<edge> G[1000006];
vector<edge> rG[1000006];
long long d[1000006];
void dijikstra(int s,int type){
    priority_queue<node> que;
    for(int i=1;i<=p;i++){
        d[i]=INF;
    }
    d[s]=0;
    que.push(node(0,s));
    while(!que.empty()){
        node temp=que.top();
        que.pop();
        int v=temp.to;
        if(d[v]<temp.dis)
            continue;
        if(type==1){
            for(int i=0;i<G[v].size();i++){
                edge e=G[v][i];
                 if(d[e.to]>d[v]+e.cost){
                    d[e.to]=d[v]+e.cost;
                    que.push(node(d[e.to],e.to));
                }
            }
        }else{
            for(int i=0;i<rG[v].size();i++){
                edge e=rG[v][i];
                if(d[e.to]>d[v]+e.cost){
                    d[e.to]=d[v]+e.cost;
                    que.push(node(d[e.to],e.to));
                }
            }
        }
    }
}
int main(){
    // ios::sync_with_stdio(false);
    // cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        scanf("%d%d",&p,&q);
        int s,e,w;
        memset(G,0,sizeof(G));
        memset(rG,0,sizeof(rG));
        for(int i=0;i<q;i++){
            scanf("%d%d%d",&s,&e,&w);
            G[s].push_back(edge(e,w));
            rG[e].push_back(edge(s,w));
        }
        dijikstra(1,1);
        long long sum=0;
        for(int i=1;i<=p;i++){
            sum+=d[i];
        }
        dijikstra(1,2);
        for(int i=1;i<=p;i++){
            sum+=d[i];
        }
        cout<<sum<<endl;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/GarrettWale/p/11404040.html