所有节点对最短路径 超时 优先队列 + dijkstra + 遍历前驱子图

贴代码在此,有待后续改进。

#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<stack>
#include<queue>
using namespace std;
#define maxn 2010
#define INF 0xffffff
int w[maxn][maxn] ;
int n  , scanfk;
struct Node{
    int id, dist ;
     bool operator<(const Node &b)const{// 重定义优先级,以小为先,按升序排列
        return dist > b.dist ;
    }
};

priority_queue<Node>q ;
int pre[maxn] ;
int visited[maxn] ;
Node node[maxn] ;

void Dijkstra(int s){
    for(int i =1 ; i<= n ; i++){
        node[i].id = i ;
        node[i].dist = 999999 ;
        pre[i] = -1 ;
        visited[i] = 0 ;
    }
        node[s].dist = 0 ;
        q.push(node[s]) ;
        while(!q.empty()){
            Node now = q.top() ;
            q.pop() ;
            int nowid = now.id ;
            if(visited[nowid])
                continue ;
            visited[nowid] = 1 ;
            for(int j = 1 ; j<= n ; j++){
                if(!visited[j] && node[j].dist > now.dist + w[nowid][j]){
                    node[j].dist = now.dist + w[nowid][j] ;
                    pre[j] = nowid ;
                    q.push(node[j]) ;
                }
            }
        }
}
int ma[maxn] ;
int kk ;
int  printD(int s, int v){   //打印从源点s到结点v的一条最短路径上的所有结点。
  if(v == s){
   // printf("%d" , s)  ;
    ma[kk++] = s ;
    }
  //else if(pre[v] == -1 )
   // printf("no path \n") ;
  else{
        printD( s, pre[v]) ;
       // printf("%d" , v) ;
        ma[kk++] = v ;
  }
  return kk -1 ;
}
int len(int ll){  // 计算从1到ll数组的相互距离
    int sumlen = 0 ;
    for(int i = 1 ; i<= ll ; i++){
        sumlen += 2 * node[ma[i]].dist ;
    }
    for(int i = 2 ; i <= ll-1 ; i++){
        sumlen += 2 * w[ma[i]][ma[i+1]] ;
    }
    return sumlen ;
}
int main()
{
    int t ;
    int  m , a, b, c , newsta[maxn] ;
    scanf("%d" , &t) ;
    while( t--){
        scanf("%d%d" , &n , &scanfk) ;
        while(!pp.empty()) pp.pop() ;
        memset(newsta , 999999 , sizeof(newsta) ) ;
        for(int i = 1 ; i<= n ;i++){
            for(int j =1  ; j<= n ; j++){
                if(i == j)
                    w[i][j]= 0 ;
                else w[i][j] = w[j][i] = 99999 ;
            }
    }
    if(n == 0  && m == 0) break ;
    for(int i = 1 ; i<= n -1 ; i++){
        scanf("%d%d%d" , &a , &b , &c) ;
        w[a][b] = w[b][a] = c ;
    }
    int g = 0  ;

    for(int i =1 ; i<= n  ; i++){
        Dijkstra(i) ;
        for(int j = i + 1 ; j<= n  ;j++){
            kk = 1 ;
            memset(ma , 0 , sizeof(ma)) ;
            int le = printD( i , j) ;
            //cout << "=" << i << ":" << j << endl ;
            if(le < scanfk)
                continue ;
            newsta[g++] = len(scanfk) ;
        }
    }
    sort(newsta , newsta + g ) ;
     cout <<  newsta[0] << endl;

    }
    return 0;
}

Cities

 
 Accepts: 73  
 Submissions: 390
 Time Limit: 4000/2000 MS (Java/Others)  
 Memory Limit: 32768/32768 K (Java/Others) Problem Description

Long long ago,there is a knight called JayYe.He lives in a small country.This country is made up of n cities connected by n-1 roads(that means it’s a tree).The king wants to reward JayYe because he beats the devil and save the princess.The king decide to give JayYe exactly K cities as his daughter’s dowry. Here comes the question.Although JayYe beats the devil,his knee was injured.So he doesn’t want to move too much,he wants his citys as close as possible,that is, JayYe wants the expected distance of his cities as small as possible. The expected distance is defined as the expected distance between node u and node v,both u and v are randomly choose from JayYe’s K cities equiprobably(that means first choose u randomly from JayYe’s K cities,then choose v randomly from JayYe’s K cities,so the case u equals to v is possible). Suppose you are the king,please determine the K cities so that JayYe is happy. Because the author is lazy,you only need tell me the minimum expect distance.

Input

The first line contains a single integer T,indicating the number of test cases. Each test case begins with two integers n and K,indicating the number of cities in this country,the number of cities the king gives to the knight.Then follows n-1 lines,each line contains three integers a,b,and c, indicating there is a road connects city a and city b with length c.

[Technical Specification] 1 <= T <= 100 1 <= K <= min(50,n) 1 <= n <= 2000 1 <= a,b <= n 0 <= c <= 100000

Output

For each case, output one line, contain one integer, the minimum expect distance multiply K2.

Sample Input

1
2 2
1 2 1

Sample Output

2

    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/zn505119020/article/details/45915043
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞