百度之星初赛第三场 1002 最短路2 (Floyd算法优化)

题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=863&pid=1002

题目:

解题报告:

  根据题目给的数据范围,用Floyd算法优化可以过,图可能存在重边,所以要取权值最小的边。

AC代码:

 1 #include<bits/stdc++.h>
 2 #define numm ch-48
 3 #define pd putchar(' ')
 4 #define pn putchar('\n')
 5 #define pb push_back
 6 #define fi first
 7 #define se second
 8 #define fre1 freopen("1.txt","r",stdin)
 9 #define fre2 freopen("3.txt","w",stdout)
10 #define bug cout<<"*******************"<<endl;
11 #define debug(args...) cout<<#args<<"->"<<args<<"\n";
12 using namespace std;
13 template <typename T>
14 void read(T &res) {
15     bool flag=false;char ch;
16     while(!isdigit(ch=getchar())) (ch=='-')&&(flag=true);
17     for(res=numm;isdigit(ch=getchar());res=(res<<1)+(res<<3)+numm);
18     flag&&(res=-res);
19 }
20 template <typename T>
21 void write(T x) {
22     if(x<0) putchar('-'),x=-x;
23     if(x>9) write(x/10);
24     putchar(x%10+'0');
25 }
26 typedef long long ll;
27 typedef unsigned long long ull;
28 const int maxn=1010;
29 const int maxm=505;
30 const int mod=1e9+7;
31 const int inv2=500000004;
32 const int inf=0x3f3f3f3f;
33 const ll INF=0x3f3f3f3f3f3f3f3f;
34 //const int N=32;
35 ll A[maxn][maxn];
36 ll a[maxn][maxn];
37 int N;
38 void Floyd_1(){
39     for(int k = 1; k <= N; k++){
40         for(int i = 1; i <= N; i++){
41             ll t = A[i][k];
42             if(t!=INF)
43                 for(int j = 1; j <= i; j++) {
44                     if(t+A[k][j]<A[i][j]) {
45                         A[i][j]=t+A[k][j];
46                         A[j][i]=A[i][j];
47                         a[i][j]=max(a[i][j],(ll)k);
48                         a[j][i]=max(a[j][i],(ll)k);
49                     }
50                 }
51         }
52     }
53     return ;
54 }
55 int main()
56 {
57 //    #define local
58     #ifdef local
59         fre1;
60         fre2;
61     #endif // local
62     int _,m;
63     read(_);
64     while(_--) {
65         read(N),read(m);
66         for(int i=1;i<=N;i++) {
67             for(int j=1;j<=N;j++)
68                 A[i][j]=(i==j?0:INF),a[i][j]=0;
69         }
70         for(int i=1;i<=m;i++) {
71             int u,v;
72             ll w;
73             read(u);read(v);read(w);
74             A[u][v]=min(w,A[u][v]);
75             A[v][u]=min(w,A[v][u]);
76         }
77         Floyd_1();
78         ll ans=0;
79         const ll mod=998244353;
80         for(int i=1;i<=N;i++)
81             for(int j=1;j<=N;j++)
82                 ans=(ans+a[i][j])%mod;
83         write(ans);pn;
84     }
85     return 0;
86 }
代码在这里!

 

posted @ 2019-08-26 21:18  wuliking  阅读(240)  评论(0编辑  收藏  举报