POJ-1511 Invitation Cards (单源最短路+逆向)

<题目链接>

题目大意:

有向图,求从起点1到每个点的最短路然后再回到起点1的最短路之和。

解题分析:

在求每个点到1点的最短路径时,如果仅仅只是遍历每个点,对它们每一个都进行一次最短路算法,那么即使是用了堆优化的dijkstra,时间复杂度也高达O(n^2 logn),而本题有1000000个点,毫无疑问,这种想法必然是不可行的,所以我们可以采用逆向思维,将图中的每一条有向边全部反向,然后以1为起点,仅做一次dijkstra,就能得到1到所有点的最短距离,即反向前的,所有点到1点的最短距离。所以,本题的正解应为:先以1为起点,做一次dijkstra,算出,1到所有点的最短距离,然后将边反向,再以1为起点,做一次dijkstra,此时就能得到,其他所有点到1的最短距离,将所有的最短距离相加,即为答案。时间复杂度为O(nlogn)。

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;

#define INF 0x3f3f3f3f
const int maxn =1000000+100;

int n,m;
struct Edge{
    int to;
    int next;
    int w;
};

Edge edge[maxn],redge[maxn];

struct NODE{
    int index;
    int dis;
    bool operator < (NODE const &tmp)const{
        return dis>tmp.dis;
    }
}d[maxn];

int dist[maxn];
int cnt,rcnt,head1[maxn],head2[maxn],vis[maxn];

void init(){
    memset(head1,-1,sizeof(head1));
    memset(head2,-1,sizeof(head2));
    cnt=0,rcnt=0;
}

void add1(int u,int v,int w){
    edge[cnt].to=v;edge[cnt].w=w;
    edge[cnt].next=head1[u];
    head1[u]=cnt++;
}

void add2(int u,int v,int w){
    redge[rcnt].to=v;redge[rcnt].w=w;
    redge[rcnt].next=head2[u];
    head2[u]=rcnt++;
}

void dijkstra1(int st){
    for(int i=1;i<=n;i++){
        vis[i]=0;d[i].dis=INF,d[i].index=i;
    }

    priority_queue<NODE>q;
    d[st].dis=0;q.push(d[st]);
    while(!q.empty()){
        int u=q.top().index;
        q.pop();
        if(vis[u])continue;
        vis[u]=1;
        for(int i=head1[u];i!=-1;i=edge[i].next){
            int v=edge[i].to;
            if(d[v].dis>d[u].dis+edge[i].w){
                d[v].dis=d[u].dis+edge[i].w;
                q.push(d[v]);
            }
        }
    }
}

void dijkstra2(int st){           //因为正、反向边的edge[],和head[]散组不同,所以要将另外再写一个dijkstra函数
    for(int i=1;i<=n;i++){
        vis[i]=0;d[i].dis=INF,d[i].index=i;
    }

    priority_queue<NODE>q;
    d[st].dis=0;q.push(d[st]);
    while(!q.empty()){
        int u=q.top().index;
        q.pop();
        if(vis[u])continue;
        vis[u]=1;
        for(int i=head2[u];i!=-1;i=redge[i].next){
            int v=redge[i].to;
            if(d[v].dis>d[u].dis+redge[i].w){
                d[v].dis=d[u].dis+redge[i].w;
                q.push(d[v]);
            }
        }
    }
}

int main(){
    int t;scanf("%d",&t);
    while(t--){
        scanf("%d %d",&n,&m);
        init();
        for(int i=1;i<=m;i++){
            int a,b,c;
            scanf("%d %d %d",&a,&b,&c);
            add1(a,b,c);          //存储该有向图正确的边
            add2(b,a,c);          //将该有向图的所有边反向存储
        }

        long long sum=0;

        dijkstra1(1);        //边未反向之前,求出1到所有点的最短路
        for(int i=2;i<=n;i++){
            sum+=d[i].dis;
        }

        dijkstra2(1);       //将边反向后,求出所有点到1点的最短路
        for(int i=2;i<=n;i++){
            sum+=d[i].dis;
        }
        printf("%lld\n",sum);
    }
    return 0;
}

2018-08-27

原文地址:https://www.cnblogs.com/00isok/p/9545360.html

时间: 2024-07-30 07:26:22

POJ-1511 Invitation Cards (单源最短路+逆向)的相关文章

POJ 1511 Invitation Cards (最短路)

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 19215   Accepted: 6311 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They wan

[2016-04-05][POJ][1511][Invitation Cards]

时间:2016-04-05 12:57:22 星期二 题目编号:[2016-04-05][POJ][1511][Invitation Cards] 题目大意:给定一个有向图,从点1出发,分别到各个站点后,又回到点1,问最少需要多少车费, 分析: 从1跑一次最短路,然后矩阵转置,再跑一次最短路,两次求和 这里不能用邻接矩阵保存,所以改成邻接表,然后矩阵转置的操作变成重新加一次边 遇到的问题:用vector存图超时,改用数组实现 #include <queue> #include <algo

POJ 1511 Invitation Cards

题目来源:http://poj.org/problem?id=1511 题目很长,花了不少时间才理解题意,目的就是为了求出来回两次最小路径(即为本题的差旅费)之和, 第一次从CCS(1)出发到各个点路径最小,SPFA算法没得说,回来时终点是确定的都是CCS(1),相当于把路 径反过来,即把有向图去反方向,又是从1出发到各个点路径最小,再用一个SPFA.注意ans要用long long 不然也WA,这个地方WA了好几次,虽然更改后AC了,但还是不明白,题目明明写了smaller than 1000

poj 1511 Invitation Cards (最短路)

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 33435   Accepted: 11104 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They wa

POJ 1511 Invitation Cards 【最短路,spfa算法,Dijkstra算法堆优化】

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 25219   Accepted: 8346 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They wan

HDU 1535 &amp;&amp; POJ 1511 Invitation Cards (SPFA 模板 + 反向建图)

Invitation Cards HDU: Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) POJ: Time Limit: 8000 MS     Memory Limit: 262144 K       Problem Description In the age of television, not many people attend theater performa

DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. POJ 3268 //#include <bits/stdc++.h> #include <cstdio> #include <queue> #include <algorithm> #include <cstring> using namespace

poj 1511 Invitation Cards (spfa+邻接表)

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 19527   Accepted: 6375 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They wan

POJ 1511 Invitation Cards 图论题解

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

(简单) POJ 1511 Invitation Cards,SPFA。

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