poj 1511 Invitation Cards(dijstra优化)

题目链接:http://poj.org/problem?id=1511

题意:给出n个点和n条有向边,求所有点到源点1的来回最短路之和(保证每个点都可以往返源点1)

题目比较简单就是边和点的个数有点多所以可以用dijstra+优先队列这样复杂度就可以到v*logn

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#define inf 1000000000
using namespace std;
const int M = 1e6 + 10;
int n , m , a[M] , b[M] , c[M] , dis[M];
struct TnT {
    int v , w;
};
struct cmp {
    bool operator() (int x , int y) {
        return dis[x] > dis[y];
    }
};
vector<TnT>vc[M];
bool vis[M];
void dij(int s) {
    priority_queue<int , vector<int> , cmp>q;
    memset(vis , false , sizeof(vis));
    TnT gg;
    q.push(s);
    dis[s] = 0;
    while(!q.empty()) {
        int m = q.top();
        vis[m] = true;
        for(int i = 0 ; i < vc[m].size() ; i++) {
            gg = vc[m][i];
            if(dis[m] + gg.w < dis[gg.v]) {
                dis[gg.v] = dis[m] + gg.w;
                if(!vis[gg.v]) {
                    vis[gg.v] = true;
                    q.push(gg.v);
                }
            }
        }
        q.pop();
    }
}
int main() {
    int t;
    TnT gg;
    scanf("%d" , &t);
    while(t--) {
        scanf("%d%d" , &n , &m);
        for(int i = 1 ; i <= n ; i++) {
            vc[i].clear();
            dis[i] = inf;
        }
        for(int i = 1 ; i <= m ; i++) {
            scanf("%d%d%d" , &a[i] , &b[i] , &c[i]);
            gg.v = b[i] , gg.w = c[i];
            vc[a[i]].push_back(gg);
        }
        dij(1);
        long long sum = 0;
        for(int i = 1 ; i <= n ; i++) {
            sum += (long long)dis[i];
        }
        for(int i = 1 ; i <= n ; i++) {
            vc[i].clear();
            dis[i] = inf;
        }
        for(int i = 1 ; i <= m ; i++) {
            gg.v = a[i] , gg.w = c[i];
            vc[b[i]].push_back(gg);
        }
        dij(1);
        for(int i = 1 ; i <= n ; i++) {
            sum += (long long)dis[i];
        }
        printf("%lld\n" , sum);
    }
    return 0;

}
时间: 2024-08-25 05:36:31

poj 1511 Invitation Cards(dijstra优化)的相关文章

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

[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 (最短路)

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

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。

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+邻接表)

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