标签:des style color os strong io for art
Description
Input
Output
Sample Input
6 6 1 2 1 2 3 1 3 4 1 4 5 1 1 5 2 4 6 3 1 6 2 4 0 0
Sample Output
3 Hint: One possible arrangement is (1-2-3-4-6) for Wukong and (2-3-4) for Tang Monk. The number of common points are 3.
要求最多的点重合,也就是说要在相同的最短路内尽量用更多的点,定义数组dis[i][j]表示i到j的最短距离,定义mm[i][j]表示i到j的最短距离需要几个点。相同的最短距离要求最多的点,弗洛伊德算法,求解每个点的最短路和需要的点数,遍历路线(i,j)使得 dis[a][b] = dis[a][i] + dis[i][j] + dis[j][b] ;满足条件的路线选取更多的点。
#include <cstdio> #include <cstring> #define INF 0x3f3f3f3f #include <algorithm> using namespace std; int dis[310][310] , mm[310][310] ; int main() { int i , j , k , n , m , a , b , c , d ; while(~scanf("%d %d", &n, &m)) { if(n == 0 && m == 0) break; memset(dis,INF,sizeof(dis)); memset(mm,0,sizeof(mm)); while(m--) { scanf("%d %d %d", &i, &j, &k); if( dis[i][j] > k ) { dis[i][j] = dis[j][i] = k ; mm[i][j] = mm[j][i] = 1 ; } } scanf("%d %d %d %d", &a, &b, &c, &d); for(i = 1 ; i <= n ; i++) { dis[i][i] = 0 ; mm[i][i] = 0 ; } for(k = 1 ; k <= n ; k++) for(i = 1 ; i <= n ; i++) for(j = 1 ; j <= n ; j++) if( dis[i][j] > dis[i][k] + dis[k][j] || ( dis[i][j] == dis[i][k] + dis[k][j] && mm[i][j] < mm[i][k] + mm[k][j] ) ) { dis[i][j] = dis[i][k] + dis[k][j] ; mm[i][j] = mm[i][k] + mm[k][j] ; } int ans = -1 ; for(i = 1 ; i <= n ; i++) for(j = 1 ; j <= n ; j++) if( mm[i][j] > ans && dis[a][b] == dis[a][i] + dis[i][j] + dis[j][b] && dis[c][d] == dis[c][i] + dis[i][j] + dis[j][d] ) ans = mm[i][j] ; printf("%d\n", ans+1); } return 0; }
标签:des style color os strong io for art
原文地址:http://blog.csdn.net/winddreams/article/details/38348963