Find someone n of friends:
For a small group (a set of data), a is b, friend, friendliness of X; b is c, friends, friendliness is Y; and a and c are not friends, then said c 2 degrees friend a, friendly degree of x + y; b is 1 degree friend of a, friendliness of x;
now enter data:
line 1: T, representative of a total of n sets of data, such as below 2;
the first 2 to 3 lines: respectively enter different parameters of the first data
row 2: m, I, n-;
m = 10 represent this group of 10 people, each person numbered 0 ~. 9;
I =. 5 Representative seek No. 5, human buddy relationship;
n-2 = request on behalf of friend 2 No. 5, no output 1; if, in accordance with the descending order friendliness, if friendliness same, arranged in ascending order of the number of people ;
line 3: k = 13 the first number represents the first group of a total of 13 friend relationship, the degree of friendship 0 3 5 0 3 and No. 5, and so on;
output in sequence: i-th of the n of friends, if T is equal to 2 then the two relationships to be output;
2 10. 5 2 13 is omitted later 5 ... 0. 3
C ++ code:
#include<iostream> #include<vector> #include<string> #include<queue> #include<algorithm> using namespace std; bool cmp(vector<int> a,vector<int> b){ return a[1]>b[1]; } int main(){ int T; cin>>T; for(int id=0;id<T;id++){ int m,i,n; cin>>m>>i>>n; int k; cin>>k; //朋友圈图初始化 vector<vector<int> > g(m,vector<int>(m,0)); for(int x=0;x<k;x++){ int a,b,w; cin>>a>>b>>w; g[a][b]=g[b][a]=w; } vector<int> visit(m,0); queue<int> q; //Initialization of a friend for ( int X = 0 ; X <m; X ++ ) { IF (G [I] [X] =! 0 {) q.push (X); Visit [X] = G [I] [X ]; } } // BFS // first layer, an n-degree find friends for ( int X = . 1 ; X <n; X ++ ) { int len = q.size (); // a second layer, the current End of friends to find, friends of y; for ( int y = 0 ; y <len; y ++ ) { int a =q.front (); q.pop (); // a third layer of the current x-Looking next degree all friends for ( int Z = 0 ; Z <m; Z ++ ) { IF ! (Z = I G && [A] [Z] =! 0 && Visit [Z] == 0 ) { q.push (Z); Visit [Z] = Visit [A] + G [A] [Z]; } IF (Visit ! [Z] = 0 && Visit [Z] <Visit [A] + G [A] [Z]) Visit [Z] = Visit [A] + G [A] [Z]; } } } Vector < int > ndf; if(q.size()==0){ cout<<-1<<endl;continue; }else{ while(!q.empty()){ ndf.push_back(q.front()); q.pop(); } } int max_x=ndf.size(); vector<vector<int> > res; sort(ndf.begin(),ndf.end()); for(int x=0;x<max_x;x++){ vector<int> cell; cell.push_back(ndf[x]); cell.push_back(visit[ndf[x]]); res.push_back(cell); } sort(res.begin(),res.end(),cmp); for(int x=0;x<max_x;x++){ cout<<res[x][0]<<" "; } cout<<endl; } return 0; } /* 2 10 5 2 13 0 3 5 0 4 9 0 6 8 0 7 5 1 2 6 1 6 3 2 9 7 3 4 3 3 5 3 3 8 3 3 9 3 5 8 9 7 8 9 10 0 2 13 0 3 5 0 4 9 0 6 8 0 7 5 1 2 6 1 6 3 2 9 7 3 4 3 3 5 3 3 8 3 3 9 3 5 8 9 7 8 9 */