挺好的一道题
想到对于任意两点(可相同),如果两路径都经过那么一定是走同样的两点之间的最短路
这道题就迎刃而解啦
代码如下:
/* ^^ ====== ^^ ID: meixiuxiu PROG: test LANG: C++11 */ #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include <cstring> #include <climits> #include <string> #include <vector> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> #include <sstream> #include <cctype> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int ,int> pii; #define MEM(a,b) memset(a,b,sizeof a) #define CLR(a) memset(a,0,sizeof a); #define pi acos(-1.0) #define maxn 40000 #define maxv 100005 const int inf = 0x3f3f3f3f; const int MOD = 1e9 + 7; //#define LOCAL int dis[505][505]; int cnt[505][505]; int n,m; int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); // freopen("out.txt","w",stdout); #endif while(cin >> n >> m && (n+m)){ MEM(dis,inf); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cnt[i][j] = 2; } } for(int i=1;i<=n;i++)dis[i][i]=0,cnt[i][i]=1; for(int i=1;i<=m;i++){ int a,b,c;scanf("%d%d%d",&a,&b,&c); dis[a][b] = min(dis[a][b],c); dis[b][a] = min(dis[b][a],c); } int s1,t1,s2,t2;scanf("%d%d%d%d",&s1,&t1,&s2,&t2); for(int k=1;k<=n;k++){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(dis[i][j] > dis[i][k]+dis[k][j]){ dis[i][j] = dis[i][k]+dis[k][j]; cnt[i][j] = cnt[i][k]+cnt[k][j]-1; } else if(dis[i][j] == dis[i][k]+dis[k][j] && cnt[i][j]<cnt[i][k]+cnt[k][j]-1){ cnt[i][j] = cnt[i][k]+cnt[k][j]-1; } } } } int nmax = 0; ll a = dis[s1][t1]; ll b = dis[s2][t2]; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if((((dis[s1][i]+dis[i][j]+dis[j][t1])==a) || ((dis[t1][i]+dis[i][j]+dis[j][s1])==a)) && (((dis[s2][i]+dis[i][j]+dis[j][t2])==b) || ((dis[t2][i]+dis[i][j]+dis[j][s2])==b))){ nmax = max(nmax,cnt[i][j]); } } } end:cout << nmax <<endl; } return 0; }