这个是我的ac代码,这时候数组下标开的特别大
#include<bits/stdc++.h>
using namespace std;
int n;
struct Edge{
int to,next,val;
}edge[25000];
int head[20000],dis[20000],ans,tot=1,up[20000];
bool vis[25000],school[5200];
inline void addEdge(int a,int b){
edge[++tot].next = head[a];
edge[tot].to = b;
edge[tot].val = 1;
head[a] = tot;
edge[++tot].next = head[b];
edge[tot].to = a;
edge[tot].val = 0;
head[b] = tot;
return ;
}
int be,en;
bool dfs(){
for(int i = 0;i<=en;i++){
vis[i] = false;
}
queue<int> q;
q.push(be);
vis[be] = true;
dis[be] = INT_MAX;
while(!q.empty()){
int x = q.front();
q.pop();
for(int i = head[x];i;i=edge[i].next){
if(!edge[i].val) continue;
int v = edge[i].to;
if(vis[v]) continue;
q.push(v);
up[v] = i;
dis[v] = min(dis[x],edge[i].val);
vis[v] = true;
if(v==en){
return true;
}
}
}
return false;
}
void update(){
int x = en;
while(x!=be){
int v = up[x];
edge[v].val -= dis[en];
edge[v^1].val += dis[en];
x = edge[v^1].to;
}
ans += dis[en];
}
void init(){
memset(edge,0,sizeof(edge));
tot = 1;
memset(head,0,sizeof(head));
ans = 0;
memset(school,false,sizeof(school));
return ;
}
int main(){
int T;
cin >> T;
while(T--){
addEdge(1,2);
init();
cin >> n;
be = 0;
en = 2*n+1;
int totstu = 0;
for(int i =1;i<=n;i++){
bool isSchool;
cin >> isSchool;
school[i] = isSchool;
if(isSchool){
addEdge(n+i,en);
}
}
for(int i = 1;i<=n;i++){
bool isHome;
cin >> isHome;
if(!school[i]){
totstu++;
addEdge(be,i);
continue;
}
if(!isHome){
totstu++;
addEdge(be,i);
}
}
for(int i = 1;i<=n;i++){
for(int j = 1;j<=n;j++){
bool isFriend;
cin >> isFriend;
if(i==j){
addEdge(i,n+i);
continue;
}
if(isFriend){
addEdge(i,n+j);
}
}
}
while(dfs()){
update();
}
if(ans==totstu){
cout << "^_^" << endl;
}else{
cout <<"T_T"<<endl;
}
}
return 0;
}
但是,当我调小数组下标之后到300,根据题目要求,内存是充足的(最多有2*n+1个点),可是除了前三个AC之外,后面的数据就开始五颜六色的了
30分 TLE代码
#include<bits/stdc++.h>
using namespace std;
int n;
struct Edge{
int to,next,val;
}edge[300];
int head[300],dis[300],ans,tot=1,up[300];
bool vis[300],school[520];
inline void addEdge(int a,int b){
edge[++tot].next = head[a];
edge[tot].to = b;
edge[tot].val = 1;
head[a] = tot;
edge[++tot].next = head[b];
edge[tot].to = a;
edge[tot].val = 0;
head[b] = tot;
return ;
}
int be,en;
bool dfs(){
for(int i = 0;i<=en;i++){
vis[i] = false;
}
queue<int> q;
q.push(be);
vis[be] = true;
dis[be] = INT_MAX;
while(!q.empty()){
int x = q.front();
q.pop();
for(int i = head[x];i;i=edge[i].next){
if(!edge[i].val) continue;
int v = edge[i].to;
if(vis[v]) continue;
q.push(v);
up[v] = i;
dis[v] = min(dis[x],edge[i].val);
vis[v] = true;
if(v==en){
return true;
}
}
}
return false;
}
void update(){
int x = en;
while(x!=be){
int v = up[x];
edge[v].val -= dis[en];
edge[v^1].val += dis[en];
x = edge[v^1].to;
}
ans += dis[en];
}
int main(){
int T;
cin >> T;
while(T--){
addEdge(1,2);
memset(edge,0,sizeof(edge));
tot = 1;
memset(head,0,sizeof(head));
ans = 0;
memset(school,false,sizeof(school));
cin >> n;
be = 0;
en = 2*n+1;
int totstu = 0;
for(int i =1;i<=n;i++){
bool isSchool;
cin >> isSchool;
school[i] = isSchool;
if(isSchool){
addEdge(n+i,en);
}
}
for(int i = 1;i<=n;i++){
bool isHome;
cin >> isHome;
if(!school[i]){
totstu++;
addEdge(be,i);
continue;
}
if(!isHome){
totstu++;
addEdge(be,i);
}
}
for(int i = 1;i<=n;i++){
for(int j = 1;j<=n;j++){
bool isFriend;
cin >> isFriend;
if(i==j){
addEdge(i,n+i);
continue;
}
if(isFriend){
addEdge(i,n+j);
}
}
}
while(dfs()){
update();
}
if(ans==totstu){
cout << "^_^" << endl;
}else{
cout <<"T_T"<<endl;
}
}
return 0;
}
求教大犇出现问题的原因,谢谢
我估计原因是我的init函数开错了,希望大佬找找原因,最大流和建图应该无问题(不然也不能ac)