题目描述

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. 刷题总结——蜥蜴(ssoj网络流)

    题目: 题目背景 SCOI2007 DAY1 T3 题目描述 在一个 r 行 c 列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外.每行每列中相邻石柱的距 ...

  2. spring5响应式编程

    1.Spring5新特性    2.响应式编程响应式编程:非阻塞应用程序,借助异步和事件驱动还有少量的线程垂直伸缩,而非横向伸缩(分布式集群)当Http连接缓慢的时候,从数据库到Http数据响应中也会 ...

  3. tomcat设置去项目路径

    1. 新建一个目录专门用于存放工程, 如: G:\apache-tomcat-6.0.20\project 2. 将工程存放到以上目录下:即:G:\apache-tomcat-6.0.20\proje ...

  4. mybatis配置报错(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?)

    如下报错:解决方案:要按照提示的顺序添加属性,(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrappe ...

  5. uva 11997 优先队列

    K Smallest Sums You're given k arrays, each array has k integers. There are kk ways to pick exactly ...

  6. 简单说明PHP的垃圾收集机制是怎样的?

    腾讯 对变量有个引用计数,计数到0时变量被销毁. ———————————————————————— 每一种语言都有自己的自动垃圾回收机制,让程序员不必过分关心程序内存分配,但是在OOP中,有些对象需要 ...

  7. HDU 1394 线段树求逆序对

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  8. 详解DNS,你真的懂吗?

    what`s  this ? 概念 域名系统(英文:DomainNameSystem,缩写:DNS)是互联网的一项服务.它作为将域名和IP地址相互映射的一个分布式数据库,能够使人更方便地访问互联网.D ...

  9. chmod u g x o 777

    chmod [ugoa] [+-= ] [rwx] 文件或者是目录u:表示文件的属主,g:表文件的属组内的成员,o:则表示其它用户,a:是所有用户的(ugo的总和)+—=:是对权限的操作,+表示增加相 ...

  10. 某考试 T1 line

    状压dp+矩阵转移,据说正解是dfs出的合法状态,,但难道不是三个for就行了吗2333 #include<iostream> #include<cmath> #include ...