QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#504776#9102. Zayin and Elementsikun#WA 2ms5660kbC++203.1kb2024-08-04 15:53:022024-08-04 15:53:02

Judging History

你现在查看的是最新测评结果

  • [2024-08-04 15:53:02]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:5660kb
  • [2024-08-04 15:53:02]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
#define Acode ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const int N = 1e5 + 10;
const int M = 2e6 + 10;
const int inf = 0x3f3f3f3f3f3f3f3f;
int n, m, s, t;
struct Mcmf
{
    int s, t, vtot;
    int head[N], etot;
    int dis[N], flow, cost;
    int pre[N];
    bool vis[N];

    struct edge
    {
        int v, nxt;
        int f, c; // f代表流量,c代表单位费用
    } e[M * 2];
    void addedge(int u, int v, int f, int c, int f2 = 0)
    {
        e[etot] = {v, head[u], f, c};
        head[u] = etot++;
        e[etot] = {u, head[v], f2, -c};
        head[v] = etot++;
    }
    bool spfa()
    {
        int mx = inf / 2;
        for (int i = 1; i <= vtot; i++)
        {
            dis[i] = mx;
            vis[i] = false;
            pre[i] = -1;
        }
        dis[s] = 0;
        vis[s] = true;
        queue<int> q;
        q.push(s);
        while (!q.empty())
        {
            int u = q.front();
            for (int i = head[u]; ~i; i = e[i].nxt)
            {
                int v = e[i].v;
                if (e[i].f && dis[v] > dis[u] + e[i].c)
                {
                    dis[v] = dis[u] + e[i].c;
                    pre[v] = i;
                    if (!vis[v])
                    {
                        vis[v] = 1;
                        q.push(v);
                    }
                }
            }
            q.pop();
            vis[u] = false;
        }
        return dis[t] != mx;
    }
    void dfs()
    {
        int u = t;
        int f = inf;
        while (~pre[u])
        {
            f = min(f, e[pre[u]].f); 
            u = e[pre[u] ^ 1].v;  
        }
        flow += f;
        cost += f * dis[t];
        u = t;
        while (~pre[u]) 
        {
            e[pre[u]].f -= f;
            e[pre[u] ^ 1].f += f;
            u = e[pre[u] ^ 1].v;
        }
    }
    pair<int, int> mcmf()
    {
        flow = 0;
        cost = 0;
        while (spfa())
            dfs();
        return {flow, cost};
    }

    void init(int s_, int t_, int vtot_)
    {
        s = s_;
        t = t_;
        vtot = vtot_;
        etot = 0;
        for (int i = 1; i <= vtot; i++)
            head[i] = -1;
    }
};

Mcmf g;
void solve(){

	std::cin>>n>>m;

	s=2*n+3*m+1,t=s+1;
	g.init(s,t,t);//!!!

	int sum=0;
	for(int i=1;i<=m;i++){
		int a,b,c; 
		cin>>a>>b>>c;
		sum+=b*2+c*2;
		g.addedge(s,(i-1)*3+1,a,0);
		g.addedge(s,(i-1)*3+2,2*b,0);
		g.addedge(s,(i-1)*3+3,c,0);
		//g.addedge(s,i,a+b+c,0);
		int k; 
		cin>>k;
		for(int j=1;j<=k;j++){
			int x; 
			cin>>x;
			g.addedge((i-1)*3+1,3*m+x,1,0);
			g.addedge((i-1)*3+2,3*m+x,1,1);
			g.addedge((i-1)*3+3,3*m+x,1,2);
		}
	}
	for(int i=1;i<=n;i++){
		g.addedge(i+3*m,t,1,0);
	}

	g.mcmf();
	int res=g.flow;
	if(res!=n) {
		cout<<-1<<endl;
	}
	else 
	cout<<(sum-g.cost)/2<<'\n';

}

signed main(){
	Acode; 
	int T=1; std::cin>>T;
	while(T--) {
		solve();
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 5660kb

input:

2
5
2
2 3 1 2 3 1
1 1 1 3 4 5 2
5
2
2 3 1 1 3
1 0 1 2 1 2

output:

5
-1

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 5660kb

input:

5
9
10
0 0 10 2 3 8
0 0 10 2 4 6
0 8 2 2 2 4
0 0 10 3 1 3 8
0 4 6 2 2 3
0 8 2 3 2 6 7
0 9 1 2 1 7
0 2 8 3 1 4 9
0 7 3 1 5
0 0 10 3 5 6 9
3
10
0 9 1 1 2
0 5 5 1 1
0 1 9 1 1
0 7 3 1 1
0 7 3 0
0 0 10 0
0 6 4 1 3
0 9 1 0
0 7 3 0
0 9 1 1 2
90
20
0 6 4 12 1 4 5 22 32 64 66 67 73 87 88 89
0 1 9 12 3 8 21 3...

output:

95
98
155
151
152

result:

wrong answer 1st lines differ - expected: '94', found: '95'