Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
4.4 kB
0
Indexable
Never
import java.util.*;
 
class PC {
    int id;
    int level;
    Set<File> sharedFiles = new HashSet<>();
    ArrayList<Link> links = new ArrayList<>();
    Link tmpLink;
 
    public PC(int id) {
        this.id = id;
    }
}
 
class Link {
    PC from;
    PC to;
    BandWidth bandWidth;
 
    public Link(PC from, PC to, BandWidth bandWidth) {
        this.from = from;
        this.to = to;
        this.bandWidth = bandWidth;
    }
}
 
class File {
    int fileSize;
 
    public File(int mFilesize) {
        this.fileSize = mFilesize;
    }
}
 
class BandWidth {
    int total;
    int remain;
 
    public BandWidth(int bandWidth) {
        total = bandWidth;
        remain = bandWidth;
    }
}
 
class Request {
    File file;
    ArrayList<PC> pcs = new ArrayList<>();
    ArrayList<BandWidth> bandWidths = new ArrayList<>();
}
 
class UserSolution {
 
    PC[] pcs;
    HashMap<String, File> fileHashMap;
    HashMap<String, Link> linkHashMap;
    Request[] requests;
 
    public void init(int N, int mUID1[], int mUID2[], int mBandwidth[]) {
        fileHashMap = new HashMap<>();
        linkHashMap = new HashMap<>();
        requests = new Request[5001];
        pcs = new PC[N + 1];
        for (int i = 0; i <= N; i++) {
            pcs[i] = new PC(i);
        }
        for (int i = 0; i < N - 1; i++) {
            BandWidth bandWidth = new BandWidth(mBandwidth[i]);
            Link link = new Link(pcs[mUID1[i]], pcs[mUID2[i]], bandWidth);
            pcs[mUID2[i]].links.add(link);
            Link link2 = new Link(pcs[mUID2[i]], pcs[mUID1[i]], bandWidth);
            pcs[mUID1[i]].links.add(link2);
 
        }
 
    }
 
    public int share(int mUID, char mFilename[], int mFilesize) {
        File file = fileHashMap.get(charArrayToString(mFilename));
        if (file == null)
            file = new File(mFilesize);
        fileHashMap.put(charArrayToString(mFilename), file);
        pcs[mUID].sharedFiles.add(file);
        return pcs[mUID].sharedFiles.size();
    }
 
    public int request(int mUID, char mFilename[], int mTID) {
        Request request = new Request();
        Queue<PC> queue = new ArrayDeque<>();
        File file = fileHashMap.get(charArrayToString(mFilename));
        if (file == null) {
            return -1;
        }
        queue.add(pcs[mUID]);
        pcs[mUID].level = 0;
        PC from = new PC(1000);
        from.level = 1000;
        boolean[] mark = new boolean[201];
        while (!queue.isEmpty()) {
            PC pc = queue.poll();
            mark[pc.id] = true;
            if (pc.level > from.level) {
                break;
            }
            if (pc.sharedFiles.contains(file)) {
                from.level = pc.level;
                if (pc.id < from.id) {
                    from = pc;
                }
            }
            for (Link link : pc.links) {
 
                if (link.bandWidth.remain >= file.fileSize) {
                    if (!mark[link.from.id]) {
                        queue.add(link.from);
                        link.from.level = pc.level + 1;
                        link.from.tmpLink = link;
                    }
                }
 
 
            }
        }
        if (from.id == 1000) return -1;
        PC tmp = from;
        request.file = file;
        while (true) {
            request.bandWidths.add(tmp.tmpLink.bandWidth);
            tmp.tmpLink.bandWidth.remain -= file.fileSize;
            tmp = tmp.tmpLink.to;
            request.pcs.add(tmp);
            if (tmp.id == mUID)
                break;
        }
        requests[mTID] = request;
        return from.id;
    }
 
    public int complete(int mTID) {
        Request request = requests[mTID];
 
        for (PC pc : request.pcs) {
            pc.sharedFiles.add(request.file);
        }
        for (BandWidth b : request.bandWidths) {
            b.remain += request.file.fileSize;
        }
 
        return request.pcs.get(request.pcs.size() - 1).sharedFiles.size();
    }
 
    String charArrayToString(char[] chars) {
        String result = "";
        for (char c : chars
        ) {
            if (c == '\0') {
                break;
            }
            result += c;
        }
        return result;
    }
}
Leave a Comment