百度之星--最短路 2--思维+最短路

最短路 2

Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 687    Accepted Submission(s): 176


 

Problem Description

小 A 是社团里的工具人,有一天他的朋友给了他一个 n 个点,m 条边的正权连通无向图,要他计算所有点两两之间的最短路。

作为一个工具人,小 A 熟练掌握着 floyd 算法,设 w[i][j] 为原图中 (i,j) 之间的权值最小的边的权值,若没有边则 w[i][j]= 无穷大。特别地,若 i=j ,则 w[i][j]=0 。

Floyd 的 C++ 实现如下:

```c++
for(int k=1;k<=p;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
  w[i][j]=min(w[i][j],w[i][k]+w[k][j]);
```

当 p=n 时,该代码就是我们所熟知的 floyd ,然而小 A 为了让代码跑的更快点,所以想减少 p 的值。

令 Di,j 为最小的非负整数 x ,满足当 p=x 时,点 i 与点 j 之间的最短路被正确计算了。

现在你需要求 ∑ni=1∑nj=1Di,j ,虽然答案不会很大,但为了显得本题像个计数题,你还是需要将答案对 998244353 取模后输出。

 

 

Input

第一行一个正整数 T(T≤30) 表示数据组数

对于每组数据:

第一行两个正整数 n,m(1≤n≤1000,m≤2000) ,表示点数和边数。

保证最多只有 5 组数据满足 max(n,m)>200

接下来 m 行,每行三个正整数 u,v,w 描述一条边权为 w 的边 (u,v) ,其中 1≤w≤109

 

 

Output

输出 T 行,第 i 行一个非负整数表示第 i 组数据的答案

 

 

Sample Input

 

1 4 4 1 2 1 2 3 1 3 4 1 4 1 1

 

 

Sample Output

 

6

 

 

Source

2019 年百度之星·程序设计大赛 - 初赛三

 

 

Recommend

liuyiding

 

 

每次在松弛的时候记录中断点。

当距离小时,会更新中断点,或者是当距离=原先保存的最小值时,看max(a[s][x],x)是否小于a[s][to],更新一下。

#include <algorithm>    //STL通用算法
#include <bitset>     //STL位集容器
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>     //复数类
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>      //STL双端队列容器
#include <exception>    //异常处理类
#include <fstream>
#include <functional>   //STL定义运算函数(代替运算符)
#include <limits>
#include <list>      //STL线性列表容器
#include <map>       //STL 映射容器
#include <iomanip>
#include <ios>      //基本输入/输出支持
#include<iosfwd>     //输入/输出系统使用的前置声明
#include <iostream>
#include <istream>     //基本输入流
#include <ostream>     //基本输出流
#include <queue>      //STL队列容器
#include <set>       //STL 集合容器
#include <sstream>    //基于字符串的流
#include <stack>      //STL堆栈容器    
#include <stdexcept>    //标准异常类
#include <streambuf>   //底层输入/输出支持
#include <string>     //字符串类
#include <utility>     //STL通用模板类
#include <vector>     //STL动态数组容器
#include <cwchar>
#include <cwctype>
#define ll long long
using namespace std;
//priority_queue<ll,vector<ll>,less<ll> >q;
ll dx[]= {-1,1,0,0,-1,-1,1,1};
ll dy[]= {0,0,-1,1,-1,1,1,-1};
const ll maxn = 2000+66;
const ll mod=998244353;
const ll inf=99999999999;
ll n,m;
ll mas[maxn][maxn];
struct edge
{
    ll to;
    ll next;
    ll v;
};
struct Dij
{
    edge edge[maxn*2];
    ll cnt,head[maxn*2],n;
    ll dis[maxn];
    void init(ll nn)
    {
        n=nn;
        cnt=0;
        for(ll i=0; i<=n; i++)
        {
            head[i]=0;
        }
    }
    void add(ll u,ll v,ll w)
    {
        cnt++;
        edge[cnt].to=v;
        edge[cnt].next=head[u];
        head[u]=cnt;
        edge[cnt].v=w;
    }

    void ans(ll s)
    {
        priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>> >q;
        while(q.size())
            q.pop();
        ll i,now;
        for (i=1; i<=n; i++)
            dis[i]=inf;
        dis[s]=0;
        q.push(make_pair(0,s));
        //cout<<s<<"++"<<endl;
        while (!q.empty())
        {
            now=q.top().second;
            q.pop();
            for (i=head[now]; i; i=edge[i].next)
            {
                ll to=edge[i].to;
                //cout<<to<<"------"<<endl;
                if (dis[now]+edge[i].v<dis[edge[i].to]||dis[now]+edge[i].v==dis[edge[i].to]&&max(mas[s][now],now)<mas[s][to])
                {

                    ll tmp=max(mas[s][now],now);
                    //cout<<tmp<<"--"<<endl;
                    if(tmp!=s&&tmp!=to)
                    {
                        mas[s][to]=tmp;
                        //mas[to][s]=tmp;
                        //cout<<tmp<<"--"<<s<<" "<<to<<endl;
                    }
                    dis[edge[i].to]=dis[now]+edge[i].v;
                    q.push(make_pair(dis[edge[i].to],edge[i].to));
                }
            }
        }
    }
    void solve()
    {
        for(ll i=1; i<=n; i++)
        {
            ans(i);
            //cout<<mas[1][2]<<"!"<<endl;
        }

    }

} D;
int main()
{
    ll t;
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld %lld",&n,&m);
        D.init(n);
        for(ll i=1; i<=n+10; i++)
        {
            for(ll j=1; j<=n+10; j++)
            {
                mas[i][j]=0;
            }
        }
        for(ll i=1; i<=m; i++)
        {
            ll u,v;
            ll w;
            scanf("%lld %lld %lld",&u,&v,&w);
            D.add(u,v,w);
            D.add(v,u,w);
        }
        D.solve();
        ll num=0;
        //cout<<mas[1][2]<<"!"<<endl;
        for(ll i=1; i<=n; i++)
        {
            for(ll j=i+1; j<=n; j++)
            {
                num+=mas[i][j];
                num%=mod;
            }
        }
        printf("%I64d\n",(2*num)%mod);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值