poj1511 最短路

题意:与poj3268一样,所有人需要从各点到一点再从一点到各点,求最短路总和。

与poj3268一样,先正向建图跑 dijkstra ,得到该点到其他所有各点的最短路,即这些人回去的最短路,再用反向建的图跑一遍最短路,得到各点到该点的最短路,求和就行了。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<queue>
 5 #include<vector>
 6 using namespace std;
 7 typedef pair<int,int> pii;
 8 typedef long long ll;
 9 const int MAXM=1000000;
10 struct cmp{
11     bool operator ()(pii a,pii b){
12         return a.first>b.first;
13     }
14 };
15
16 int head1[MAXM+5],point1[MAXM+5],val1[MAXM+5],next1[MAXM+5],size1;
17 int head2[MAXM+5],point2[MAXM+5],val2[MAXM+5],next2[MAXM+5],size2;
18 int n,m;
19 int dist1[MAXM+5],dist2[MAXM+5];
20
21 void add1(int a,int b,int v){
22     point1[size1]=b;
23     val1[size1]=v;
24     next1[size1]=head1[a];
25     head1[a]=size1++;
26 }
27
28 void add2(int a,int b,int v){
29     point2[size2]=b;
30     val2[size2]=v;
31     next2[size2]=head2[a];
32     head2[a]=size2++;
33 }
34
35 void dij(int s){
36     int i;
37     priority_queue<pii,vector<pii>,cmp>q;
38     memset(dist1,0x3f,sizeof(dist1));
39     memset(dist2,0x3f,sizeof(dist2));
40     dist1[s]=dist2[s]=0;
41     q.push(make_pair(dist1[s],s));
42     while(!q.empty()){
43         pii u=q.top();
44         q.pop();
45         if(u.first>dist1[u.second])continue;
46         for(i=head1[u.second];~i;i=next1[i]){
47             int j=point1[i];
48             if(dist1[j]>dist1[u.second]+val1[i]){
49                 dist1[j]=dist1[u.second]+val1[i];
50                 q.push(make_pair(dist1[j],j));
51             }
52         }
53     }
54     while(!q.empty())q.pop();
55     q.push(make_pair(dist2[s],s));
56     while(!q.empty()){
57         pii u=q.top();
58         q.pop();
59         if(u.first>dist2[u.second])continue;
60         for(i=head2[u.second];~i;i=next2[i]){
61             int j=point2[i];
62             if(dist2[j]>dist2[u.second]+val2[i]){
63                 dist2[j]=dist2[u.second]+val2[i];
64                 q.push(make_pair(dist2[j],j));
65             }
66         }
67     }
68     ll ans=0;
69     for(i=1;i<=n;i++){
70         ans+=dist1[i];
71         ans+=dist2[i];
72     }
73     printf("%I64d\n",ans);
74 }
75
76 int main(){
77     int t;
78     scanf("%d",&t);
79         for(int q=1;q<=t;q++){
80             scanf("%d%d",&n,&m);
81             int i,a,b,v;
82             memset(head1,-1,sizeof(head1));
83             size1=0;
84             memset(head2,-1,sizeof(head2));
85             size2=0;
86             for(i=1;i<=m;i++){
87                 scanf("%d%d%d",&a,&b,&v);
88                 add1(a,b,v);
89                 add2(b,a,v);
90             }
91             dij(1);
92         }
93
94     return 0;
95 }

时间: 2024-08-28 14:05:24

poj1511 最短路的相关文章

poj1511 最短路 spfa

// poj1511 最短路 spfa // // Bellman-Ford 队列优化 // // 留个spfa模板,精髓就是不断松弛,并将可能会影响 // 结果的点,如果在队列中不用加,不在就加入. #include <cstdio> #include <algorithm> #include <cstring> #include <iostream> #include <queue> typedef long long ll; using n

POJ1511(最短路大数据处理)

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

ACM/ICPC 之 最短路-SPFA+正逆邻接表(POJ1511(ZOJ2008))

求单源最短路到其余各点,然后返回源点的总最短路长,以构造邻接表的方法不同分为两种解法. POJ1511(ZOJ2008)-Invitation Cards 改变构造邻接表的方法后,分为两种解法 解法一: 1 //POJ1511-ZOJ2008 2 //Time:7766Ms Memory:99112K 3 //求从1处到各点后再返回1处的最短总路长 4 //需要构造邻接表和逆邻接表 5 //构造方法1:vector构造邻接表 6 //SPFA+邻接表 7 #include<iostream>

poj1511/zoj2008 Invitation Cards(最短路模板题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds      Memory Limit: 65536 KB In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fa

POJ1511 Invitation Cards(多源单汇最短路)

边取反,从汇点跑单源最短路即可. 1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<algorithm> 5 using namespace std; 6 #define MAXN 1111111 7 #define MAXM 1111111 8 9 inline void in(int &ret){ 10 char c; ret=0; 11 while(c=get

POJ-1511 Invitation Cards( 最短路,spfa )

题目链接:http://poj.org/problem?id=1511 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 ha

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 (单源最短路+逆向)

<题目链接> 题目大意: 有向图,求从起点1到每个点的最短路然后再回到起点1的最短路之和. 解题分析: 在求每个点到1点的最短路径时,如果仅仅只是遍历每个点,对它们每一个都进行一次最短路算法,那么即使是用了堆优化的dijkstra,时间复杂度也高达O(n^2 logn),而本题有1000000个点,毫无疑问,这种想法必然是不可行的,所以我们可以采用逆向思维,将图中的每一条有向边全部反向,然后以1为起点,仅做一次dijkstra,就能得到1到所有点的最短距离,即反向前的,所有点到1点的最短距离.

POJ1511:Invitation Cards(最短路)

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 34743   Accepted: 11481 题目链接:http://poj.org/problem?id=1511 Description: In the age of television, not many people attend theater performances. Antique Comedians of Malidi