其他分享
首页 > 其他分享> > H.Holy Grail ( floyd )(The Preliminary Contest for ICPC Asia Nanjing 2019)

H.Holy Grail ( floyd )(The Preliminary Contest for ICPC Asia Nanjing 2019)

作者:互联网

题意:

给出一个有向图,再给出6条原来不存在的路径,让你在这6条路径上添加一个最小的数,使图不存在负环。

思路:

直接6遍 floyd 输出就行了。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 typedef pair<int,int> PII;
 5 #define lson l,mid,rt<<1
 6 #define rson mid+1,r,rt<<1|1
 7 const int inf=(1<<29);
 8 const int mod=1e9+7;
 9 const int N=1e6+10;
10 int n,m;
11 
12 ll dp[505][505];
13 int a,b;
14 ll c;
15 int main()
16 {
17     int T;
18     scanf("%d", &T);
19     while (T--) {
20         memset(dp,0x3f3f3f,sizeof dp);
21 
22         scanf("%d%d",&n,&m);
23         for (int i = 1; i <= m; i++) {
24             scanf("%d%d%lld",&a,&b,&c);
25             dp[a][b]=c;
26         }
27 
28         for (int i = 1; i <= 6; i++)
29         {
30             scanf("%d%d",&a,&b);
31             for (int l = 0; l <n; l++)
32                 for (int j = 0; j < n; j++)
33                     for (int k = 0; k < n; k++)
34                         dp[j][k]=min(dp[j][l]+dp[l][k],dp[j][k]);
35             printf("%lld\n",-dp[b][a]);
36             dp[a][b]=-dp[b][a];
37         }
38     }
39     return 0;
40 }

 

标签:typedef,题意,Holy,Contest,路径,long,floyd,Asia,给出
来源: https://www.cnblogs.com/--HPY-7m/p/11443171.html