标签:关系 amp comment utc long 尺寸 tor sqrt void
考虑到矩阵尺寸的关系,可以枚举起点和终点,并且判断是否可行即可。
判断起点和终点是否可以通过挖空至多T个障碍联通。实际上就是求起点到终点的最短路。
所以我们先建好图,然后求以每个方格为起始点的最短路,复杂度O(n*m^2*logm).
枚举起点和终点更新答案的复杂度是O(n^2*m^2).
总复杂度就是(nm(nm+mlogm)).
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi 3.1415926535 # define eps 1e-9 # define MOD 100000007 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; int Scan() { int res=0, flag=0; char ch; if((ch=getchar())==‘-‘) flag=1; else if(ch>=‘0‘&&ch<=‘9‘) res=ch-‘0‘; while((ch=getchar())>=‘0‘&&ch<=‘9‘) res=res*10+(ch-‘0‘); return flag?-res:res; } void Out(int a) { if(a<0) {putchar(‘-‘); a=-a;} if(a>=10) Out(a/10); putchar(a%10+‘0‘); } const int N=905; //Code begin... struct qnode{ int v, c; qnode(int _v=0, int _c=0):v(_v),c(_c){} bool operator <(const qnode &r)const{return c>r.c;} }; struct Edge{int p, next, w;}edge[N*8]; int head[N], cnt=1, dis[N], G[N][N]; char s[35][35]; bool vis[N]; void add_edge(int u, int v, int w){ edge[cnt].p=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++; } void Dijkstra(int n, int start){ mem(vis,0); FO(i,0,n) dis[i]=INF; priority_queue<qnode>que; while (!que.empty()) que.pop(); dis[start]=0; que.push(qnode(start,0)); qnode tmp; while (!que.empty()) { tmp=que.top(); que.pop(); int u=tmp.v; if (vis[u]) continue; vis[u]=true; for (int i=head[u]; i; i=edge[i].next) { int v=edge[i].p, cost=edge[i].w; if (!vis[v]&&dis[v]>dis[u]+cost) dis[v]=dis[u]+cost, que.push(qnode(v,dis[v])); } } } int main () { int n, m, T; scanf("%d%d%d",&n,&m,&T); FO(i,0,n) scanf("%s",s[i]); FO(i,0,n) FO(j,0,m) { if (j!=m-1) add_edge(i*m+j,i*m+j+1,s[i][j+1]==‘1‘), add_edge(i*m+j+1,i*m+j,s[i][j]==‘1‘); if (i!=n-1) add_edge(i*m+j,i*m+j+m,s[i+1][j]==‘1‘), add_edge(i*m+j+m,i*m+j,s[i][j]==‘1‘); } double ans=0; FO(i,0,n) FO(j,0,m) { Dijkstra(n*m,i*m+j); FO(k1,0,n) FO(k2,0,m) { G[i*m+j][k1*m+k2]=dis[k1*m+k2]+(s[i][j]==‘1‘); if (G[i*m+j][k1*m+k2]>T) continue; ans=max(ans,sqrt((i-k1)*(i-k1)+(j-k2)*(j-k2))); } } printf("%.6f\n",ans); return 0; }
标签:关系 amp comment utc long 尺寸 tor sqrt void
原文地址:http://www.cnblogs.com/lishiyao/p/6561157.html