标签:ica only one bee return ref ffffff help can
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3174 Accepted Submission(s):
1325
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 1100 #define MAXM MAXN*MAXN #define INF 0x7fffffff using namespace std; queue<int>que; int t,n,d,tot,kase; int dis[MAXN],vis[MAXN],num[MAXN],id; int to[MAXM],net[MAXM],cap[MAXM],head[MAXN]; struct House{ int high;int no; }arr[MAXN]; int cmp(House a,House b){ return a.high<b.high; } void add(int u,int v,int w){ to[++tot]=v;cap[tot]=w;net[tot]=head[u];head[u]=tot; } int spfa(int s,int t,int n){ memset(num,0,sizeof(num)); memset(vis,0,sizeof(vis)); memset(dis,0x7f,sizeof(dis)); while(!que.empty()) que.pop(); dis[s]=0;vis[s]=1;que.push(s); while(!que.empty()){ int now=que.front(); que.pop(); vis[now]=0;num[now]++; if(num[now]>n) return -1; for(int i=head[now];i;i=net[i]){ if(dis[to[i]]>dis[now]+cap[i]){ dis[to[i]]=dis[now]+cap[i]; if(!vis[to[i]]){ vis[to[i]]=1; que.push(to[i]); } } } } return dis[t]; } int main(){ scanf("%d",&t); while(t--){ memset(head,0,sizeof(head)); scanf("%d%d",&n,&d);tot=0; for (int i=1;i<=n;i++){ scanf("%d",&arr[i].high); arr[i].no=i; if(i!=n) add(i+1,i,-1); } sort(arr+1,arr+n+1,cmp); for(int i=1;i<n;i++){ int u=arr[i].no; int v=arr[i+1].no; if(u>v) swap(u,v); add(u,v,d); } int u=arr[1].no,v=arr[n].no; if(u>v) swap(u,v); printf("Case %d: %d\n",++kase,spfa(u,v,n)); } }
标签:ica only one bee return ref ffffff help can
原文地址:https://www.cnblogs.com/cangT-Tlan/p/9091588.html