Graph theory (Floyd algorithm): NOI2007 social Network

Source: Internet
Author: User

[NOI2007] Social networks

★ Import File: network1.in output file: network1.out Simple comparison
Time limit: 1 s memory limit: MB

"Problem description"

In the study of social networks (social network), we often use the concept of graph theory to explain some social phenomena. Let's look at a question like this. There are n individuals in a social circle, and there are different levels of relationships between people. We map this relationship network to an n-node graph, and two different people who know each other, connect a non-aligned edge between their corresponding nodes, and attach a positive value of c,c, which indicates that the relationship between the two people is more close.

We can use the shortest distance between the corresponding nodes to measure the close degree of the relationship between two people s and T, noting that the other nodes on the quickest path provide some convenience for the link between s and T, that is, these nodes have a certain degree of importance to the connection between S and T. We can measure the importance of this node in social networks by counting the number of shortest paths through a node v.

Consider that there may be more than one shortest path between two nodes A and B. The definition of the importance of our revision is as follows:

The cs,t represents the number of different shortest-circuiting from S to T, and Cs,t (v) indicates the number of shortest circuits passing through V from S to t;

I(V)=∑S≠V,T≠VCS,T(V)st

Is the degree of importance of node V in social networks.

To make sense for I (v) and Cs,t (v), we stipulate that the social networks to be processed are connected undirected graphs, that is, the shortest path of a finite length between any two nodes.

Now give a weighted graph describing the social network s, and ask you to find out how important each node is.

"Input File"

The first line in the input file has two integers, N and m, representing the number of nodes and non-forward edges in the social network. In the no-map, we numbered all the nodes from 1 to N.

Next m line, each line with three integers a, B, C describes a connection node A and B, the weight of C is a non-forward edge. Note that there is a maximum of one non-tangential edge between any two nodes, and no self-loops are present in the graph (i.e., the two endpoints that do not have a non-forward edge are the same nodes).

"Output File"

The output file consists of n rows, one real number per line, and 3 digits after the decimal point. The real number on line I indicates how important node I is in social networks.

"Sample Input"

4 4

1 2 1

2 3 1

3 4 1

4 1 1

"Sample Output"

1.000

1.000

1.000

1.000

"Sample description"

Social networks as shown.

For the 1th junction, only the shortest path between the 2nd and 4th nodes and the 4th to 2nd Junction is the number 1th junction, and the shortest line between the 2nd Junction and the 4th Junction has 2 points. Thus, according to the definition, the importance degree of the number 1th junction is calculated as 1/2+1/2=1. Due to the symmetry of the graph, the other three nodes are also of 1 importance.

"Scoring Method"

There is no part of the point, only when your program calculates the importance of each node and the standard output difference of not more than 0.001, to get the test points, otherwise do not score.

"Data size and conventions"

      • 50% of data: n≤10,m≤45
      • 100% of the data: N≤100,m≤4 500, the weight of any one side C is a positive integer, satisfies: 1≤c≤1 000.
      • All data are guaranteed to be given undirected graphs, and the number of shortest paths between any two nodes does not exceed 10^10.

Floyd algorithm violence can be, need to think clearly to their own situation.

1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 Const intmaxn= the;6 Long LongG[MAXN][MAXN];7 Long LongE[MAXN][MAXN];8 intn,m;9 intMain () {Ten #ifndef Online_judge OneFreopen ("network1.in","R", stdin); AFreopen ("Network1.out","W", stdout); - #endif -scanf"%d%d",&n,&m); the      for(intI=1; i<=n;i++) -          for(intj=1; j<=n;j++){ -E[i][j]=i==j?0:(Long Long) 1e18; -g[i][j]=1; +         } -      for(intI=1, a,b,c;i<=m;i++){ +scanf"%d%d%d",&a,&b,&c); AE[a][b]=c; e[b][a]=C; atg[a][b]=g[b][a]=1; -     } -      -      for(intk=1; k<=n;k++) -          for(intI=1; i<=n;i++) -              for(intj=1; j<=n;j++){ in                 if(i==k| | k==j| | I==J)Continue; -                 if(e[i][k]+e[k][j]<E[i][j]) { toe[i][j]=e[i][k]+E[k][j]; +g[i][j]=g[i][k]*G[k][j]; -                 } the                 Else if(e[i][k]+e[k][j]==E[i][j]) *g[i][j]+=g[i][k]*G[k][j]; $             }Panax Notoginseng      -     Doubleans=0; the      for(intx=1; x<=n;x++){ +ans=0.0;  A          for(intI=1; i<=n;i++) the              for(intj=1; j<=n;j++) +                 if(i!=j&&i!=x&&j!=x&&e[i][x]+e[x][j]==E[i][j]) -ans+=1.0*g[i][x]*g[x][j]/G[i][j];  $printf"%.3f\n", ans); $     } -     return 0; -}

Graph theory (Floyd algorithm): NOI2007 social Network

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.