程序设计:最短路(2020蓝桥杯省赛B组模拟题)

给定一个 n 点 m 边的有向带权图表示一座城市,起点为 1 。送餐小哥需要给 n 个客户送外卖,第 i 个客户的家在第 i 号点。由于他的车子容量很小,所以一次只能容纳一份外卖,所以送达外卖之后就要回到起点取新的外卖送下一单,直到全部送到位置。

有向图保证联通。外卖小哥一定走的最短路。

求送餐小哥走的总路程。

输入格式
第一行一个整数 T,表示数据组数。

对于每组数据,第一行两个整数 n 和 m 。

接下来 mm 行,每行三个整数 ui,vi,ci
表示每条有向边。

输出格式
对于每组数据,输出一行一个整数表示答案。

数据范围
对于 20%20% 的数据: 0 < n \leq 1000<n≤100 ,

对于 40%40% 的数据: 0 < n \leq 3000<n≤300 ,

对于 60%60% 的数据: 0 < n \leq 10000<n≤1000 ,

对于 100%100% 的数据: 0 < n \leq 20000, m \leq 60000, 1\le T\le 10,0\le c_i\le 10^9,1\le u_i,v_i\le n0<n≤20000,m≤60000,1≤T≤10,0≤c
i

保证答案在 long long 范围内。

样例输入

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

样例输出

46
210

最近也是一直在写蓝桥的题,看了今年七月初赛的题,感觉还是有点难度的,所以还是准备多练几套,想在九月初赛拿个好成绩。

话不多说。这道题题意也是很明显,就是有向图求1到2-n的最短路,再求2-n到1的最短路,其实就是先正向建图,在反向建图,跑两次spfa就行了。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<bitset>
#include<vector>
#include<queue>
#include<stack>
#include<map>
using namespace std;
const int maxn = 1e6 + 100;
const int inf=0x3f3f3f3f;
typedef long long ll;
ll gcd(ll x, ll y) { return y == 0 ? x : gcd(y, x % y); }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll n, m, tol, head[maxn], dis[maxn], vis[maxn], head1[maxn];
struct node
{
    int to, cost,next;
}rode[maxn];
struct node1
{
    int to, cost,next;
}edge[maxn];
void add(ll a,ll b,ll c){//链式前向星正向建图
	rode[tol].to=b;
	rode[tol].cost=c;
	rode[tol].next=head[a];
	head[a]=tol++;
}
void add1(ll a,ll b,ll c)//反向
	edge[tol].to=b;
	edge[tol].cost=c;
	edge[tol].next=head1[a];
	head1[a]=tol++;
}
void spfa1(int s){
	queue<int>q;
    memset(dis, inf, sizeof dis);
    dis[s]=0;
	vis[s]=1;
	q.push(s);
	while(!q.empty()){
		int v=q.front();
		q.pop();
		vis[v]=0;
		for(int i=head1[v];i!=-1;i=edge[i].next)
		{
			node1 e=edge[i];
			if(dis[e.to]>dis[v]+e.cost)
			{
				dis[e.to]=dis[v]+e.cost;
				if(!vis[e.to])
				{
					q.push(e.to);
					vis[e.to]=1;
				}
			}
		}
	}
}
void spfa(int s){
	queue<int>q;
    memset(dis, inf, sizeof dis);
    dis[s]=0;
	vis[s]=1;
	q.push(s);
	while(!q.empty()){
		int v=q.front();
		q.pop();
		vis[v]=0;
		for(int i=head[v];i!=-1;i=rode[i].next)
		{
			node e=rode[i];
			if(dis[e.to]>dis[v]+e.cost)
			{
				dis[e.to]=dis[v]+e.cost;
				if(!vis[e.to])
				{
					q.push(e.to);
					vis[e.to]=1;
				}
			}
		}
	}
}
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
    tol=0;
    cin >> n >> m;
    memset(head, -1, sizeof head);
    memset(head1, -1, sizeof head1);
    for (int i = 1; i <= m;i++)
    {
        ll a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        add(a, b, c);
        add1(b, a, c);
    }
    
    ll sum = 0;
    spfa(1);
    for (int i = 2; i <= n;i++)
    {
        sum += dis[i];
    }
    tol = 0;
    spfa1(1);
    for (int i = 2; i <= n;i++)
    {
        sum += dis[i];
    }

    cout << sum << endl;
    }
    //system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值