1. 程式人生 > >CSU 1317: Find the max Link 1319: CX‘s dreams 1321: CX and girls

CSU 1317: Find the max Link 1319: CX‘s dreams 1321: CX and girls


#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>

using namespace std;

#define maxn 50011
#define INF -10000000


struct Node {
	long long son;
	long long w;
};

long long ans;
vector<Node> num[maxn];
long long d[maxn][2];

void DFS(long long x,long long p)
{ long long i,v; d[x][0]=d[x][1]=INF; for(i=0;i<num[x].size();i++) { v=num[x][i].son; if(v!=p) { DFS(v,x); if(d[v][0]>0) { if(d[x][0]<num[x][i].w+d[v][0]) { d[x][1]=d[x][0]; d[x][0]=num[x][i].w+d[v][0]; } else if(d[x][1]<num[x][i].w+d[v][0]) d[x][1
]=num[x][i].w+d[v][0]; } else { if(d[x][0]<num[x][i].w) { d[x][1]=d[x][0]; d[x][0]=num[x][i].w; } else if(d[x][1]<num[x][i].w) d[x][1]=num[x][i].w; } } } ans=max(ans,d[x][0]); ans=max(ans,d[x][0]+d[x][1]); } int main() { long long N,M; long long
i,u,v; long long w; Node tmp; while(scanf("%lld%lld",&N,&M)==2) { for(i=1;i<=N;i++) num[i].clear(); ans=-10000000; for(i=1;i<=M;i++) { scanf("%lld%lld%lld",&u,&v,&w); tmp.w=w; tmp.son=v; num[u].push_back(tmp); tmp.son=u; num[v].push_back(tmp); ans=max(ans,w); } DFS(1,-1); printf("%lld\n",ans); } return 0; } /********************************************************************** Problem: 1317 User: 3901140225 Language: C++ Result: AC Time:104 ms Memory:5900 kb **********************************************************************/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
//點標 [0,n]
typedef long long ll;
const int N = 3010;
const int M = 500010;
const ll INF = 1e18;
template<class T>
struct Max_Flow {
    int n;
    int Q[N], sign;
    int head[N], level[N], cur[N], pre[N];
    int nxt[M], pnt[M], E;
    T cap[M];
    void Init(int n) {
        this->n = n+1;
        E = 0;
        std::fill(head, head + this->n, -1);
    }
    //有向rw 就= 0
    void add(int from, int to, T c, T rw) {
        pnt[E] = to;
        cap[E] = c;
        nxt[E] = head[from];
        head[from] = E++;

        pnt[E] = from;
        cap[E] = rw;
        nxt[E] = head[to];
        head[to] = E++;
    }
    bool Bfs(int s, int t) {
        sign = t;
        std::fill(level, level + n, -1);
        int *front = Q, *tail = Q;
        *tail++ = t; level[t] = 0;
        while(front < tail && level[s] == -1) {
            int u = *front++;
            for(int e = head[u]; e != -1; e = nxt[e]) {
                if(cap[e ^ 1] > 0 && level[pnt[e]] < 0) {
                    level[pnt[e]] = level[u] + 1;
                    *tail ++ = pnt[e];
                }
            }
        }
        return level[s] != -1;
    }
    void Push(int t, T &flow) {
        T mi = INF;
        int p = pre[t];
        for(int p = pre[t]; p != -1; p = pre[pnt[p ^ 1]]) {
            mi = std::min(mi, cap[p]);
        }
        for(int p = pre[t]; p != -1; p = pre[pnt[p ^ 1]]) {
            cap[p] -= mi;
            if(!cap[p]) {
                sign = pnt[p ^ 1];
            }
            cap[p ^ 1] += mi;
        }
        flow += mi;
    }
    void Dfs(int u, int t, T &flow) {
        if(u == t) {
            Push(t, flow);
            return ;
        }
        for(int &e = cur[u]; e != -1; e = nxt[e]) {
            if(cap[e] > 0 && level[u] - 1 == level[pnt[e]]) {
                pre[pnt[e]] = e;
                Dfs(pnt[e], t, flow);
                if(level[sign] > level[u]) {
                    return ;
                }
                sign = t;
            }
        }
    }
    T Dinic(int s, int t) {
        pre[s] = -1;
        T flow = 0;
        while(Bfs(s, t)) {
            std::copy(head, head + n, cur);
            Dfs(s, t, flow);
        }
        return flow;
    }
};
Max_Flow <ll>F;

ll dream[N], work[N], ans;
int from, to;
int n, m;
const ll C = 1e6;
void input(){
	ans = 0;
	from = 0; to = n+m+1;
	F.Init(to);
	for(int i = 1; i <= n; i++)
	{
		scanf("%lld", &dream[i]);
		F.add(from, i, dream[i]*C+1LL, 0);
		ans += dream[i];
	}
	for(int i = 1; i <= m; i++)
	{
		scanf("%lld", &work[i]);
		if(work[i] >= 0) ans += work[i];
		else
			F.add(n +i, to, -work[i]*C, 0);
	}
	for(int i = 1, siz, u; i <= n; i++)
	{
		scanf("%d", &siz);
		while(siz--){
			scanf("%d", &u);
			if(work[u] >= 0)continue;
			F.add(i, n+u, INF, 0);
		}
	}
}
int main() {
	while(~scanf("%d %d", &n, &m)){
		input();
		ll flow = F.Dinic(from,to);
	//	cout<<"FLOW:"<<flow<<endl;
		cout<< ans - flow / C << " " << n-flow % C <<endl;
	}
	return 0;
}
/**********************************************************************
	Problem: 1319
	User: 3901140225
	Language: C++
	Result: AC
	Time:200 ms
	Memory:9940 kb
**********************************************************************/
#include <iostream>
#include <cstring>
#include <cstdio>
#define MAXN 1010
#define MAXM 10100
#define INF 0x3f3f3f3f
using namespace std;

int n, m;
int dis[MAXN], vis[MAXN], map[MAXN][MAXN], cost[MAXN], total[MAXN];

int dijkstra()
{
    for(int i = 2; i <= n; i++)
        dis[i] = map[1][i];
    vis[1] = 1;
    dis[1] = 0;
    total[1] = cost[1];
    for(int i = 2; i <= n; i++)
        total[i] = cost[i] + cost[1];
    for(int i = 2; i <= n; i++)
    {
        int k = -1, minx = INF;
        for(int j = 1; j <= n; j++)
        {
            if(!vis[j] && dis[j] < minx)
            {
                minx = dis[j];
                k = j;
            }
        }
        if(k == -1) break;
        vis[k] = 1;
        for(int j = 1; j <= n; j++)
        {
            if(vis[j]) continue;
            if(dis[j] > dis[k] + map[k][j])
            {
                dis[j] = dis[k] + map[k][j];
                total[j] = total[k] + cost[j];
            }
            else if(dis[j] == dis[k] + map[k][j])
            {
                if(total[j] < total[k] + cost[j])
                    total[j] = total[k] + cost[j];
            }
        }
    }
    return dis[n];
}

int main()
{
    int a, b, c;
    while(scanf("%d%d", &n, &m) != EOF)
    {
        memset(vis, 0, sizeof(vis));
        memset(map, 0x3f, sizeof(map));
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &cost[i]);
            map[i][i] = 0;
        }
        for(int i = 1; i <= m; i++)
        {
            scanf("%d%d%d", &a, &b, &c);
            if(c < map[a][b])
                map[a][b] = map[b][a] = c;
        }
        int ans = dijkstra();
        if(ans == INF) printf("-1\n");
        else printf("%d\n", total[n]);
    }
    return 0;
}
/**********************************************************************
	Problem: 1321
	User: 3901140225
	Language: C++
	Result: AC
	Time:412 ms
	Memory:6024 kb
**********************************************************************/

相關推薦

CSU 1317: Find the max Link 1319: CXs dreams 1321: CX and girls

#include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std; #define maxn 50011

SPOJ Find the max XOR value(二進制,貪心即可)

push line def pow 技術 精度問題 上界 分享 map You have two integers L and R, and you are required to find the max xor value of a and b where L <

1319CXs dreams 【最大權閉合圖 -- 最大化正點權個數】

CX有很多的夢想,但是沒有付出拿來的收穫。比如CX想擁有巧克力般的八塊腹肌,就要在健身房舉啞鈴以及跑步,這是很累的。比如CX要成為ACM大犇,就要刷題1000道,但CX是很享受刷題的,所以並不一定所有的付出都會讓CX感到很累、不喜歡。於是CX對每個夢想和每個付出

F - Find The Bone

.com col unit table 輸入 mce 每次 move each F - Find The Bone Zane the wizard is going to perform a magic show shuffling the cups. There are

關於jmeter命令行執行.jmx文件出現Error in NonGUIDriver java.lang.RuntimeException: Could not find the TestPlan class的問題

使用 lang exception ava 出現 問題 drive test bug jmeter命令行執行.jmx文件時,有時回出現Error in NonGUIDriver java.lang.RuntimeException: Could not find the T

算法(9)Find the Duplicate Number

lee leetcode 題目 但是 ron ont 拼圖遊戲 裏的 要求 一個數組中的長度是n+1,裏面存放的數字大小的範圍是【1,n】,根據鴿巢原理,所以裏面肯定有重復的數字,現在預定重復的數字就1個,讓你找到這個數字! http://bookshadow.com/we

MyEclipse運行Java出錯:could not find the main class:test.program will exit(導入項目)

沒有 fin run lib could not sys program pil 編譯器 自己新建的項目運行沒有任何問題。但是我導入的很早以前別人寫的項目,然後run就會彈框could not find the main class:test.program will ex

[LeetCode] Find the Closest Palindrome 尋找最近的回文串

bsp ren present diff absolut tco zed ret turn Given an integer n, find the closest integer (not including itself), which is a palindro

hdoj 1596 find the safest road

mission 能夠 trac tdi sub -a 代碼 tracking spf find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K

Find the squareroot

comm where ctu ref better output som pretty enc https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob

poj 1426 Find The Multiple

lines code span cti its case sig pac unsigned id=1426">poj 1426 的傳送門 Language: Find The Multiple Time Limit: 1000MS Mem

UVA - 11077 Find the Permutations (置換)

ive tinc none unsigned ati 多少 space include spec Sorting is one of the most usedoperations in real life, where Computer Science comes

HDU 1598 find the most comfortable road

改進 sort using problem clu spa mes span nbsp https://vjudge.net/problem/HDU-1598 思路:一開始想了很久才想通,先把邊進行排序,然後枚舉邊的起點和終點,但是這樣就是三重循環,t了。之後的改進,大概就

[LintCode] Find the Missing Number II

you flag discus body ted rst related chan boolean Giving a string with number from 1 to n in random order, but miss 1 number.Find that

HDOJ--1596--find the safest road

accept tom scan set -a eof 變化 head data find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K

【枚舉】【高斯消元】Gym - 101412D - Find the Outlier

void while ... () fine blog 個數 i+1 find 給你一個未知的d次多項式在0,1,...,d+2處的取值,其中有且只有一個是錯的,問你哪個是錯的。 枚舉哪個是錯的,再在剩下的d+2個中取d+1個高斯消元,解出多項式系數,然後代一下最後剩下的那

POJ 1426 Find The Multiple &amp;&amp; 51nod 1109 01組成的N的倍數 (BFS + 同余模定理)

ase 正整數 ng- eof ger put emp lan respond Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissio

hdu 1599 find the mincost route

std word esp func space hid floyd tracking oss pid=1599">click here ~~ ***find the mincost ro

hdu 1596 find the safest road

esp return clu include using one 旅遊 space ott Problem Description XX星球有很多城市,每個城市之間有一條或多條飛行通道,但是並不是所有的路都是很安全的,每一條路有一個安全系數s,s是在 0 和 1 間的實數(

Nginx錯誤:nginx: [error] OpenEvent("Global gx_reload_6252") failed (2: The system cannot find the file specified)

導致 microsoft style cif not nginx錯誤 開啟 family mic 執行nginx -s reload命令:   nginx: [error] OpenEvent("Global\ngx_reload_6252") failed (2: The