题目描述

Bobo has a directed graph G with n vertex labeled by 1,2,3,..n.

Let D(i,j) be the number of edges from vertex i to vertex j on the shortest path.

If the shortest path does not exist,then D(i,j)=n.

Bobo would like to find the sum of  D(i,j)*D(i,j) for all 1<=i<=n and 1<=j<=n.

输入

There are no more than 5 test cases.

The first line contains an integer n(1<=n<=1000).

The i-th of the following n lines contains n integers g(i,1),g(i,2),..g(i,n).

If there is an edge from i to j,then g(i,j)=1,otherwise g(i,j)=0;

输出

An integer denotes the sum of D(i,j)*D(i,j) for all 1<=i<=n and 1<=j<=n.

样例输入

3
010
001
100
2
10
01

样例输出

15
8
题意就是求所有D(i,j)*D(i,j)的和。D(i,j)代表i j之间的最短路径。
正常的想法肯定是 bfs求出任意两点之间的最短路径 但这样做的时间复杂度大概n^3 会超时。
得优化。用set维护未访问的点 因为set的删除 插入的操作都是logn 而n最大是1000。所以总体的时间复杂度是 常数*n^2
/* ***********************************************
Author :guanjun
Created Time :2016/3/21 16:44:25
File Name :neu1685.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 1010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
int n;
char mp[maxn][maxn];
int vis[maxn];
int dis[maxn];
ll sum=;
set<int>s;
set<int>::iterator it;
void solve(){
cle(dis);
s.clear();
int cnt=;
queue<int>q;
for(int i=;i<=n;i++){
q.push(i);
for(int j=;j<=n;j++){
if(i==j)continue;
if(mp[i][j]=='')dis[j]=,q.push(j);
else s.insert(j);
}
//cout<<"s "<<s.size()<<endl;
while(!q.empty()){
int x=q.front();q.pop();
cnt=;
for(it=s.begin();it!=s.end();it++){
if(mp[x][*it]==''){
q.push(*it);
dis[*it]=dis[x]+;
vis[++cnt]=*it;
}
}
for(int j=;j<=cnt;j++)s.erase(vis[j]);
}
for(int j=;j<=n;j++){
if(i==j)continue;
else{
if(dis[j]>)sum+=dis[j]*dis[j];
else sum+=n*n;
}
}
cle(dis);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
while(cin>>n){
sum=;
for(int i=;i<=n;i++){
scanf("%s",mp[i]+);
}
/*
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)
printf("%c%c",mp[i][j],j==n?10:' ');
}*/
solve();
printf("%lld\n",sum);
}
return ;
}
												

NEU 1685: All Pair Shortest Path的更多相关文章

  1. The Shortest Path in Nya Graph

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

  2. (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  3. HDU 4725 The Shortest Path in Nya Graph(构图)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  4. HDU 4725 The Shortest Path in Nya Graph (最短路)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  5. 847. Shortest Path Visiting All Nodes

    An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...

  6. 【CF938G】Shortest Path Queries(线段树分治,并查集,线性基)

    [CF938G]Shortest Path Queries(线段树分治,并查集,线性基) 题面 CF 洛谷 题解 吼题啊. 对于每个边,我们用一个\(map\)维护它出现的时间, 发现询问单点,边的出 ...

  7. Proof for Floyd-Warshall's Shortest Path Derivation Algorithm Also Demonstrates the Hierarchical Path Construction Process

    (THIS BLOG WAS ORIGINALLY WRTITTEN IN CHINESE WITH LINK: http://www.cnblogs.com/waytofall/p/3732920. ...

  8. The Shortest Path in Nya Graph HDU - 4725

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

  9. [CF843D]Dynamic Shortest Path

    [CF843D]Dynamic Shortest Path 题目大意: 给定一个带权有向图,包含\(n(n\le10^5)\)个点和\(m(m\le10^5)\)条边.共\(q(q\le2000)\) ...

随机推荐

  1. K-D tree入门

    久仰K-D tree大名已久,终于在合适的时候遇见了合适的水题入了坑入了门 K-D tree是什么 K-D tree是什么? 按名字上翻译来就是K维的树,就是一个用来维护K维空间的点的平衡二叉树 K- ...

  2. cf493E Vasya and Polynomial

    Vasya is studying in the last class of school and soon he will take exams. He decided to study polyn ...

  3. Spring-IOC源码解读2.2-BeanDefinition的载入和解析过程

    1. 对IOC容器来说这个载入过程就像是相当于把BeanDefinition定义的信息转化成Spring内部表示的数据结构.容器对bean的管理和依赖注入过程都是通过对其持有的BeanDefiniti ...

  4. C# 实现刻录光盘功能

    最近公司提出一个需求,要把公司系统的图像刻录成光盘(公司系统是医院放射科系统,很多放射科的图像) 查看了很多资料发现有两个比较可靠 1:使用IMAPI2,进行文件的光盘刻录,具体实例可以参照以下链接: ...

  5. StoryBoard中,TableView位置总是在顶部出现空白的解决

      重设TableView的 contentInset 属性可解决. 
_tableView.contentInset = UIEdgeInsetsMake( -30, 0, 0, 0);


  6. ci框架——辅助函数

    辅助函数:application/helper下面.命名要求为***_helper.php;这样在调用的时候直接$this->load->helper('***');若想给自定义的辅助函数 ...

  7. 模拟用户登录-SpringMVC+Spring+Mybatis整合小案例

    1. 导入相关jar包 ant-1.9.6.jarant-launcher-1.9.6.jaraopalliance.jarasm-5.1.jarasm-5.2.jaraspectj-weaver.j ...

  8. linux下查看cpu使用情况

    1.在终端输入top 2.ubuntu系统自带有system monitor 3.sudo apt-get install sysstat 然后在终端输入:mpstat

  9. Object中的wait,notify,notifyAll基本使用(转)

    让线程停止运行/睡眠的方法只有两个:Thread.sleep()或者obj.wait() 记住obj.nofity()并不能停止线程运行,因为notify虽然释放了锁,但依然会急促执行完synchro ...

  10. 使用crontab定时执行脚本时别忘了输出重定向>

    原文:https://blog.csdn.net/solmyr_biti/article/details/50683279 -------------------------------------- ...