2.14今日总结

上午学习了迪杰斯特拉+前向星。

P3371 【模板】单源最短路径(弱化版

题目背景

本题测试数据为随机数据,在考试中可能会出现构造数据让SPFA不通过,如有需要请移步 P4779

题目描述

如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度。

输入格式

第一行包含三个整数 n,m,sn,m,s,分别表示点的个数、有向边的个数、出发点的编号。

接下来 mm 行每行包含三个整数 u,v,wu,v,w,表示一条 u \to vu→v 的,长度为 ww 的边。

输出格式

输出一行 nn 个整数,第 ii 个表示 ss 到第 ii 个点的最短路径,若不能到达则输出 2^{31}-1231−1。

输入输出样例

输入 #1复制

4 6 1
1 2 2
2 3 2
2 4 1
1 3 5
3 4 3
1 4 4

输出 #1复制

0 2 4 3

说明/提示

【数据范围】
对于 20\%20% 的数据:1\le n \le 51≤n≤5,1\le m \le 151≤m≤15;
对于 40\%40% 的数据:1\le n \le 1001≤n≤100,1\le m \le 10^41≤m≤104;
对于 70\%70% 的数据:1\le n \le 10001≤n≤1000,1\le m \le 10^51≤m≤105;
对于 100\%100% 的数据:1 \le n \le 10^41≤n≤104,1\le m \le 5\times 10^51≤m≤5×105,1\le u,v\le n1≤u,v≤n,w\ge 0w≥0,\sum w< 2^{31}∑w<231,保证数据随机。

对于真正 100\%100% 的数据,请移步 P4779。请注意,该题与本题数据范围略有不同。

样例说明:

图片1到3和1到4的文字位置调换

思路:和普通的迪杰斯特拉差不多,但是多了前向星。

前向星:

struct node{
  int to;
  int v;
  int next;
}mp[1000];

代码实现:

#include<bits/stdc++.h>
using namespace std;
int n,m,s;
int low[10001];
int vis[10001];
const int maxm=2147483647;
struct node{
	int to;
	int v;
	int next;
}mp[500001];
int head[10001];
int cnt=0;
void add(int a,int b,int v)
{
	cnt++;
	mp[cnt].to=b;
	mp[cnt].v=v;
	mp[cnt].next=head[a];
	head[a]=cnt;
}
int main()
{
	cin>>n>>m>>s;
	int a,b,v;
	for(int i=1;i<=n;i++)
	low[i]=maxm;
	for(int i=1;i<=m;i++)
	{
		cin>>a>>b>>v;
		add(a,b,v);
	}
	vis[s]=1;
	low[s]=0;
	while(1)//迪杰斯特拉 
	{
		for(int i=head[s];i!=0;i=mp[i].next)
		{
			low[mp[i].to]=min(low[mp[i].to],low[s]+mp[i].v);
		}
	int minm=maxm;int flag=-1;
	for(int i=1;i<=n;i++)
	{
		if(vis[i]==0&&low[i]<minm)
		{
			minm=low[i];
			flag=i;
		}
	}
		if(flag==-1)
		break;
		vis[flag]=1;
		s=flag;
	}
	for(int i=1;i<=n;i++)
	cout<<low[i]<<" ";
}

P1339 [USACO09OCT]Heat Wave G

题目描述

有一个 nn 个点 mm 条边的无向图,请求出从 ss 到 tt 的最短路长度。

输入格式

第一行四个正整数 n,m,s,tn,m,s,t。 接下来 mm 行,每行三个正整数 u,v,wu,v,w,表示一条连接 u,vu,v,长为 ww 的边。

输出格式

输出一行一个整数,表示答案。

输入输出样例

输入 #1复制

7 11 5 4
2 4 2
1 4 3
7 2 2
3 4 3
5 7 5
7 3 3
6 1 1
6 3 4
2 4 3
5 6 3
7 2 1

输出 #1复制

7

说明/提示

【数据范围】
对于 100\%100% 的数据,1\le n \le 25001≤n≤2500,1\le m \le 62001≤m≤6200,1\le w \le 10001≤w≤1000。

【样例说明】
5 \to 6 \to 1 \to 45→6→1→4 为最短路,长度为 3+1+3 = 73+1+3=7。

思路:和上面那个题一样,都是用迪杰斯特拉+前向星,但是它是个无向图,所以两边都要互通。

代码实现:

#include<bits/stdc++.h>
using namespace std;
int n,m,s,t;
int low[100000];
int vis[100000];
int maxm=1e9;
struct node{
	int to;
	int v;
	int next;
}mp[100000];
int cnt=0;
int head[100000];
void add(int a,int b,int v)
{
	cnt++;
	mp[cnt].to=b;
	mp[cnt].v=v;
	mp[cnt].next=head[a];
	head[a]=cnt;
}
int main(){
	cin>>n>>m>>s>>t;
	for(int i=1;i<=n;i++)
	low[i]=maxm;
	int a,b,v;
	for(int i=1;i<=m;i++)
	{
		cin>>a>>b>>v;
		add(a,b,v);//无向图 
		add(b,a,v);
	}
	vis[s]=1;
	low[s]=0;
	while(1)
	{
		for(int i=head[s];i!=0;i=mp[i].next)
		{
			low[mp[i].to]=min(low[mp[i].to],low[s]+mp[i].v);
		}
		int minm=maxm;int flag=-1;
		for(int i=1;i<=n;i++)
		{
			if(vis[i]==0&&low[i]<minm)
			{
				minm=low[i];
				flag=i;
			}
		}
		if(flag==-1)
		break;
		vis[flag]=1;
		s=flag;
	}
	cout<<low[t];
	}

P2910 [USACO08OPEN]Clear And Present Danger S

题目描述

Farmer John is on a boat seeking fabled treasure on one of the N (1 <= N <= 100) islands conveniently labeled 1..N in the Cowribbean Sea.

The treasure map tells him that he must travel through a certain sequence A_1, A_2, ..., A_M of M (2 <= M <= 10,000) islands, starting on island 1 and ending on island N before the treasure will appear to him. He can visit these and other islands out of order and even more than once, but his trip must include the A_i sequence in the order specified by the map.

FJ wants to avoid pirates and knows the pirate-danger rating (0 <= danger <= 100,000) between each pair of islands. The total danger rating of his mission is the sum of the danger ratings of all the paths he traverses.

Help Farmer John find the least dangerous route to the treasure that satisfies the treasure map's requirement.

输入格式

* Line 1: Two space-separated integers: N and M

* Lines 2..M+1: Line i+1 describes the i_th island FJ must visit with a single integer: A_i

* Lines M+2..N+M+1: Line i+M+1 contains N space-separated integers that are the respective danger rating of the path between island i and islands 1, 2, ..., and N, respectively. The ith integer is always zero.

输出格式

* Line 1: The minimum danger that Farmer John can encounter while obtaining the treasure.

题意翻译

农夫约翰正驾驶一条小艇在牛勒比海上航行.

海上有 N(1\leq N\leq 100)N(1≤N≤100) 个岛屿,用 11 到 NN 编号.约翰从 11 号小岛出发,最后到达 NN 号小岛.

一张藏宝图上说,如果他的路程上经过的小岛依次出现了 A_1,A_2,\dots ,A_M(2\leq M\leq 10000)A1​,A2​,…,AM​(2≤M≤10000) 这样的序列(不一定相邻),那他最终就能找到古老的宝藏. 但是,由于牛勒比海有海盗出没.约翰知道任意两个岛屿之间的航线上海盗出没的概率,他用一个危险指数 D_{i,j}(0\leq D_{i,j}\leq 100000)Di,j​(0≤Di,j​≤100000) 来描述.他希望他的寻宝活动经过的航线危险指数之和最小.那么,在找到宝藏的前提下,这个最小的危险指数是多少呢?

输入输出格式

输入格式:

第一行:两个用空格隔开的正整数 NN 和 MM。

第二到第 M+1M+1 行:第 i+1i+1 行用一个整数 A_iAi​ 表示 FJ 必须经过的第 ii 个岛屿

第 M+2M+2 到第 N+M+1N+M+1 行:第 i+M+1i+M+1 行包含 NN 个用空格隔开的非负整数分别表示 ii 号小岛到第 1\dots N1…N 号小岛的航线各自的危险指数。保证第 ii 个数是 00。

输出格式 第一行:FJ 在找到宝藏的前提下经过的航线的危险指数之和的最小值。

说明 这组数据中有三个岛屿,藏宝图要求 FJ 按顺序经过四个岛屿:11 号岛屿、22 号岛屿、回到 11 号岛屿、最后到 33 号岛屿。每条航线的危险指数也给出了:航路(1,2),(2,3),(3,1)(1,2),(2,3),(3,1) 和它们的反向路径的危险指数分别是 5,2,15,2,1。

FJ 可以通过依次经过 1,3,2,3,1,31,3,2,3,1,3 号岛屿以 77 的最小总危险指数获得宝藏。这条道路满足了奶牛地图的要求 (1,2,1,3)(1,2,1,3)。我们避开了 11 号和 22 号岛屿之间的航线,因为它的危险指数太大了。

注意:测试数据中 aa 到 bb 的危险指数不一定等于 bb 到 aa 的危险指数!

Translated by @LJC00125

输入输出样例

输入 #1复制

3 4 
1 
2 
1 
3 
0 5 1 
5 0 2 
1 2 0 

输出 #1复制

7 

说明/提示

There are 3 islands and the treasure map requires Farmer John to visit a sequence of 4 islands in order: island 1, island 2, island 1 again, and finally island 3. The danger ratings of the paths are given: the paths (1, 2); (2, 3); (3, 1) and the reverse paths have danger ratings of 5, 2, and 1, respectively.

He can get the treasure with a total danger of 7 by traveling in the sequence of islands 1, 3, 2, 3, 1, and 3. The cow map's requirement (1, 2, 1, and 3) is satisfied by this route. We avoid the path between islands 1 and 2 because it has a large danger rating.

输入输出格式

输入格式:

第一行:两个用空格隔开的正整数N和M

第二到第M+1行:第i+1行用一个整数Ai表示FJ必须经过的第i个岛屿

第M+2到第N+M+1行:第i+M+1行包含N个用空格隔开的非负整数分别表示i号小岛到第1...N号小岛的航线各自的危险指数。保证第i个数是0。

输出格式

第一行:FJ在找到宝藏的前提下经过的航线的危险指数之和的最小值。

说明

这组数据中有三个岛屿,藏宝图要求FJ按顺序经过四个岛屿:1号岛屿、2号岛屿、回到1号岛屿、最后到3号岛屿。每条航线的危险指数也给出了:航路(1,2)、(2,3)、(3,1)和它们的反向路径的危险指数分别是5、2、1。

FJ可以通过依次经过1、3、2、3、1、3号岛屿以7的最小总危险指数获得宝藏。这条道路满足了奶牛地图的要求(1,2,1,3)。我们避开了1号和2号岛屿之间的航线,因为它的危险指数太大了。

注意:测试数据中a到b的危险指数不一定等于b到a的危险指数!

 思路:这题我采用Floyd。

代码实现:

#include<bits/stdc++.h>
using namespace std;
int mp[1000][1000];
int a[100000];
int main()
{
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>a[i];
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			int x;
			cin>>x;
			mp[i][j]=x;
		}
	}
	for(int k=1;k<=n;k++)
	{
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
		}
	}
	int cnt=0;
	for(int i=1;i<m;i++)
	{
		cnt+=mp[a[i]][a[i+1]];
	}
	cout<<cnt;
}

下午请假去办身份证。

晚上答辩,学习了堆优化迪杰斯特拉。

堆优化地接斯特拉,就是把寻找最短的路劲用优先队列代替了。

struct node
{
	int dis;//方向
	int v;//路劲长短
	bool operator <(const node &x)const 
	{
		return x.v<v;//使最短路劲在顶部
	 } 
};

priority_queue<node>q;
q.push((node){dis,v});
pop();

P4779 【模板】单源最短路径(标准版)

题目背景

2018 年 7 月 19 日,某位同学在 NOI Day 1 T1 归程 一题里非常熟练地使用了一个广为人知的算法求最短路。

然后呢?

100 \rightarrow 60100→60;

\text{Ag} \rightarrow \text{Cu}Ag→Cu;

最终,他因此没能与理想的大学达成契约。

小 F 衷心祝愿大家不再重蹈覆辙。

题目描述

给定一个 nn 个点,mm 条有向边的带非负权图,请你计算从 ss 出发,到每个点的距离。

数据保证你能从 ss 出发到任意点。

输入格式

第一行为三个正整数 n, m, sn,m,s。 第二行起 mm 行,每行三个非负整数 u_i, v_i, w_iui​,vi​,wi​,表示从 u_iui​ 到 v_ivi​ 有一条权值为 w_iwi​ 的有向边。

输出格式

输出一行 nn 个空格分隔的非负整数,表示 ss 到每个点的距离。

输入输出样例

输入 #1复制

4 6 1
1 2 2
2 3 2
2 4 1
1 3 5
3 4 3
1 4 4

输出 #1复制

0 2 4 3

说明/提示

样例解释请参考 数据随机的模板题

1 \leq n \leq 10^51≤n≤105;

1 \leq m \leq 2\times 10^51≤m≤2×105;

s = 1s=1;

1 \leq u_i, v_i\leq n1≤ui​,vi​≤n;

0 \leq w_i \leq 10 ^ 90≤wi​≤109,

0 \leq \sum w_i \leq 10 ^ 90≤∑wi​≤109。

本题数据可能会持续更新,但不会重测,望周知。

2018.09.04 数据更新 from @zzq

思路:这题需要用堆优化迪杰斯特拉。

代码实现:

#include<bits/stdc++.h>
using namespace std;
int n,m,s;
int vis[100010];
int low[100010];
struct edge{
	int to;
	int v;
	int next;
}mp[500010];
struct node
{
	int dis;
	int v;
	bool operator <(const node &x)const 
	{
		return x.v<v;
	 } 
};
int maxm=1e9;
int head[100010];
int cnt=0;
void add(int a,int b,int v)
{
	cnt++;
	mp[cnt].to=b;
	mp[cnt].v=v;
	mp[cnt].next=head[a];
	head[a]=cnt;
 } 
int main()
{
	cin>>n>>m>>s;
	for(int i=1;i<=n;i++)
	low[i]=maxm;
	int a,b,v;
	for(int i=1;i<=m;i++)
	{
		cin>>a>>b>>v;
		add(a,b,v); 
	 } 
	 low[s]=0;
	 priority_queue<node>q;
	 q.push((node){s,0});
	 while(q.size()!=0)
	 {		
	 	int s=q.top().dis;
	 	q.pop();
	 	if(vis[s]==1)
	 	continue;
	 	vis[s]=1;
	 	for(int i=head[s];i!=0;i=mp[i].next)
	 	{
	 		if(low[mp[i].to]>low[s]+mp[i].v)
			 {
			 	low[mp[i].to]=low[s]+mp[i].v;
			 	if(vis[mp[i].to]==0)
			 	q.push((node){mp[i].to,low[mp[i].to]});
				 }	
}
}
	  for(int i=1;i<=n;i++)
	  cout<<low[i]<<" ";  
 } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值