1 solutions

  • 0
    @ 2025-11-5 15:07:17

    C :

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int f[10010];
    int ff[10010][2];
    int hash[10010];
    int info[10010][2];
    int dp[10010];
    int cnt;
    int n, m, w;
    
    int find(int v){
    if(f[v] == v)return v;
    int F = find(f[v]);
    f[v] = F;
    return F;
    }
    
    int main()
    {
        while(scanf("%d%d%d",&n,&m,&w) != EOF){
            for(int i = 1;i <= n; i++){
                scanf("%d%d",&ff[i][0],&ff[i][1]);
            }
            for(int i = 0;i <= n; i++)f[i]=i;
            memset(hash,0,sizeof(hash));
            memset(info,0,sizeof(info));
            memset(dp,0,sizeof(dp));
            cnt = 0;
            for(int i = 0;i < m; i++){
                int a, b;
                scanf("%d%d",&a,&b);
                int fa = find(a);
                int fb = find(b);
                if(fa != fb)f[fa] = fb;
            }
            for(int i = 1;i <= n; i++)f[i] = find(i);
            for(int i = 1;i <= n; i++){
                if(hash[f[i]]==0){
                    hash[f[i]] = ++cnt;
                }
                info[hash[f[i]]][0] += ff[i][0];
                info[hash[f[i]]][1] += ff[i][1];
            }
            for(int i = 1;i <= cnt; i++){
                for(int j = w;j >= info[i][0];j--){
                    if(dp[j] < dp[j - info[i][0]] + info[i][1]){
                        dp[j] = dp[j - info[i][0]] + info[i][1];
                    }
                }
            }
            int ans = 0;
            for(int i = 1;i <= w; i++){
                if(ans < dp[i])ans = dp[i];
            }
            printf("%d\n",ans);
        }
        return 0;
    }
    

    C++ :

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    struct buy
    {
    	int jg,jz;
    };
    int n,m,w,len=0;
    int f[10001],b[10001];
    buy a[10010],c[10010];
    void init();
    int find(int);
    void bag();
    int main()
    {
    	//freopen("buy4.in","r",stdin);
    	init();
    	bag();
    	return 0;
    }
    void init()
    {
    	cin>>n>>m>>w;
    	for(int i=1;i<=n;i++) f[i]=i;
    	for(int i=1;i<=n;i++) cin>>a[i].jg>>a[i].jz;
    	int x,y,fx,fy;
    	for(int i=1;i<=m;i++)
    	{
    		cin>>x>>y;
    		fx=find(x);
    		fy=find(y);
    		if(fy!=fx)
    		{
    			a[fx].jg+=a[fy].jg;
    			a[fx].jz+=a[fy].jz;
    			f[fy]=fx;	
    		}
    		
    		//if(x!=fx) a[x].jg=0x7fffffff;
    		//if(y!=fy) a[y].jg=0x7fffffff;
    	}
    	for(int i=1;i<=n;i++)
    	{
    		if(f[i]==i)
    		{
    			len++;
    			c[len]=a[i];
    			//cout<<c[len].jz<<' '<<c[len].jg<<endl;
    		}
    	}
    }
    void bag()
    {
    	for(int i=1;i<=len;i++)
    	{
    		for(int j=w;j>=c[i].jg;j--)
    		{
    			if(c[i].jg<=w)
    			{
    				if(b[j]<b[j-c[i].jg]+c[i].jz)
    				b[j]=b[j-c[i].jg]+c[i].jz;
    			}
    		}
    	}
    	cout<<b[w]<<endl;
    }
    int find(int x)
    {
    	if(f[x]==x) return x;
    	f[x]=find(f[x]);
    	return f[x];
    }
    

    Pascal :

    const maxn=10000;
    type node=record
              fromv,endv,w:longint;
              end;
         rec=record
              c,d:longint;
              end;
    var
        v,n,i,j,k,w,m,n1,n2:longint;
        t:node;
    	total:longint;
        elist:array[1..maxn] of node;
        father:array[1..maxn] of longint;
        a,b:array[1..maxn] of rec;
        f:array[0..maxn] of longint;
    
    procedure init;
    begin
     readln(n,m,w);
     for i:=1 to n do
       readln(a[i].c,a[i].d);
     for n2:=1 to m do
      begin
       readln(j,k);
       elist[n2].fromv:=j;
       elist[n2].endv:=k;
      end;
     for i:=1 to n do  {并查集初始化}
      father[i]:=i;
    end;
    function getfather(v:integer):integer;
      begin
        if (father[v]=v) then
          getfather:=v
        else
          begin
            father[v]:=getfather(father[v]);
            getfather:=father[v];
          end;
      end;
    Procedure merge(x,y:integer);
    Begin
      x:=getfather(x);
      y:=getfather(y);
      father[x]:=y;
      a[y].c:=a[y].c+a[x].c;
      a[y].d:=a[y].d+a[x].d;
    End;
    function judge(x,y:integer):boolean;
     var fx,fy : integer;
      begin
         fx := getfather(x);
         fy := getfather(y);
         If fx=fy then  exit(true)
                     else exit(false);
      end;
    
    procedure kruskal;
    begin
     i:=1;
     while i<=m do
       begin
           if not judge(elist[i].fromv,elist[i].endv) then
    		 begin merge(elist[i].fromv,elist[i].endv);
                           inc(i);
                     end
           else inc(i);
       end;
    end;
    
    begin
     init;
     kruskal;
     fillchar(b,sizeof(b),0);
      j:=0;
     for i:=1 to n do
      if (father[i]=i) and (a[i].c<=w) then
        begin
          inc(j);
          b[j]:=a[i];
        end;
     for i:=1 to j do
       for v:=w downto b[i].c do
          if f[v-b[i].c]+b[i].d>f[v] then f[v]:=f[v-b[i].c]+b[i].d;
      writeln(f[w]);
    end.
    
    
    • 1

    Information

    ID
    16419
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By