dijkstra+链式前向星+优先队列优化

Invitation Cards POJ - 1511

Description
n-1个人从1号点出发,到剩余n-1个宣传点,然后再回到1号点汇报结果,求所有人往返路径和的最小值

Input
输入由T个案例组成。输入的第一行只包含正整数T。
接下来是N和M,1 <= N,M <= 1000000,表示N个点和连接N个点的M条边。
然后有M行,每行包括三个值U,V,W,表示从点U到点V需要W的路程。你可以假设该图连通。
注意是单向通道!!!

Output
对于每个案例,打印一行,表示路径总和的最小值。

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

**思路:**题目让我们求往返最短路的最小值,可以用dijkstra正向和反向都求一次,相加即可。
**注意:**题目数据范围太大了,用邻接矩阵存图肯定会T,所以我们选择用链式前向星存图(才发现链式前向星这么猛)但没想到的是- -,dijkstra没有用优先队列优化还是会T,草率了,后面用了优先队列+链式前向星过的- -
ac代码如下:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<map>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<set>
#include<cctype>
#include<string>
#include<stdexcept>
#include<fstream>
#include<sstream>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 10000007
#define debug() puts("what the fuck!")
#define dedebug() puts("what the fuck!!!")
#define ll long long
#define ull unsigned long long
#define speed {
    
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); };
using namespace std;
const double PI = acos(-1.0);
const int maxn = 1e6 + 50;
const int N = 35;
const int INF = 0x3f3f3f3f;
const int inf = 0xfffffff;//比INF小,防止累加爆int
const double esp_0 = 1e-6;
const double gold = (1 + sqrt(5)) / 2;
int gcd(int x, int y) {
    
    
	return y ? gcd(y, x % y) : x;
}
int n, m;
int flag;
struct node {
    
    
	int next, to, w;
}edge[maxn],redge[maxn];
int head[maxn], rhead[maxn], cnt, rcnt;
int dis[maxn], rdis[maxn], vis[maxn];
void add(int u, int v, int w) {
    
    
	edge[cnt].to = v;
	edge[cnt].next = head[u];
	edge[cnt].w = w;
	head[u] = cnt++;
}
void radd(int u, int v, int w) {
    
    
	redge[rcnt].to = v;
	redge[rcnt].next = rhead[u];
	redge[rcnt].w = w;
	rhead[u] = rcnt++;
}
void init() {
    
    
	mem(dis, INF);
	mem(rdis, INF);
	mem(head, -1);
	mem(rhead, -1);
}
struct que {
    
    
	int id, dist;
	que() {
    
    };
	que(int id, int dist) :id(id), dist(dist) {
    
    };
	bool operator<(const que& a)const{
    
    
		return dist > a.dist;
	}

};
void dijkstra(int dis[],int head[],node edge[]) {
    
    
	mem(vis, 0);
	dis[1] = 0;
	priority_queue<que>q;
	q.push(que(1, dis[1]));
	while (!q.empty()) {
    
    
		que step = q.top();
		q.pop();
		if (vis[step.id])continue;
		vis[step.id] = 1;
		for (int i = head[step.id]; i != -1; i = edge[i].next) {
    
    
			int value = edge[i].to;
			if (!vis[value] && dis[value] > dis[step.id] + edge[i].w) {
    
    
				dis[value] = dis[step.id] + edge[i].w;
				q.push(que(value, dis[value]));
			}
		}
	}
}
int main() {
    
    
	int t;
	scanf("%d", &t);
	while (t--) {
    
    
		init();
		scanf("%d%d", &n, &m);
		for (int i = 1; i <= m; ++i) {
    
    
			int u, v, w;
			scanf("%d%d%d", &u, &v, &w);
			add(u, v, w);
			radd(v, u, w);
		}
		dijkstra(dis,head,edge);
		dijkstra(rdis, rhead,redge);
		ll ans = 0;
		for (int i = 1; i <= n; ++i) {
    
    
//			printf("%d %d\n", dis[i], rdis[i]);
			ans += dis[i] + rdis[i];
		}
		printf("%lld\n", ans);
	}
	return 0;
}

未用优先队列优化的错误代码

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<map>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<set>
#include<cctype>
#include<string>
#include<stdexcept>
#include<fstream>
#include<sstream>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 10000007
#define debug() puts("what the fuck!")
#define dedebug() puts("what the fuck!!!")
#define ll long long
#define ull unsigned long long
#define speed {
    
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); };
using namespace std;
const double PI = acos(-1.0);
const int maxn = 1e6 + 50;
const int N = 35;
const int INF = 0x3f3f3f3f;
const int inf = 0xfffffff;//比INF小,防止累加爆int
const double esp_0 = 1e-6;
const double gold = (1 + sqrt(5)) / 2;
int gcd(int x, int y) {
    
    
	return y ? gcd(y, x % y) : x;
}
int n, m;
struct node {
    
    
	int next, to, w;
}edge[maxn],redge[maxn];
int head[maxn], rhead[maxn], cnt, rcnt;
int dis[maxn], rdis[maxn], vis[maxn];
void add(int u, int v, int w) {
    
    
	edge[cnt].to = v;
	edge[cnt].next = head[u];
	edge[cnt].w = w;
	head[u] = cnt++;
}
void radd(int u, int v, int w) {
    
    
	redge[rcnt].to = v;
	redge[rcnt].next = rhead[u];
	redge[rcnt].w = w;
	rhead[u] = rcnt++;
}
void init() {
    
    
	mem(dis, INF);
	mem(rdis, INF);
	mem(head, -1);
	mem(rhead, -1);
}
void dijkstra(int dis[],int head[],node edge[]) {
    
    
	mem(vis, 0);
	dis[1] = 0;
	for (int i = 1; i <= n; ++i) {
    
    
		int min = INF;
		int step = -1;
		for (int j = 1; j <= n; ++j) {
    
    
			if (!vis[j] && min > dis[j]) {
    
    
				min = dis[j];
				step = j;
			}
		}
		if (step == -1)break;
		vis[step] = 1;
		for (int i = head[step]; i != -1; i = edge[i].next) {
    
    
			int now = edge[i].to;
			if (dis[now] > dis[step] + edge[i].w) {
    
    
				dis[now] = dis[step] + edge[i].w;
			}
		}
	}
}
int main() {
    
    
	int t;
	scanf("%d", &t);
	while (t--) {
    
    
		init();
		scanf("%d%d", &n, &m);
		for (int i = 1; i <= m; ++i) {
    
    
			int u, v, w;
			scanf("%d%d%d", &u, &v, &w);
			add(u, v, w);
			radd(v, u, w);
		}
		dijkstra(dis,head,edge);
		dijkstra(rdis, rhead,redge);
		ll ans = 0;
		for (int i = 1; i <= n; ++i) {
    
    
//			printf("%d %d\n", dis[i], rdis[i]);
			ans += dis[i] + rdis[i];
		}
		printf("%lld\n", ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40924271/article/details/107763277