1. 程式人生 > >CSU 2005: Nearest Maintenance Point 2007: Football Training Camp 2017: Highest Tower

CSU 2005: Nearest Maintenance Point 2007: Football Training Camp 2017: Highest Tower

#include<cstdio>
#include<cstring>
#include<queue>
#include<bitset>
#include<vector>
#include<algorithm>
#define INF 0x3f3f3f3f
#define N 10100
using namespace std;

int d[N],m,n,s,q,a[N],ans[N];
typedef pair<int,int> P;
struct Edge{int y,w;};
vector<Edge> g[N];
bitset
<1010> p[N]; //最多1000個關鍵點,用關鍵點的序號來對應每一位 inline void dijkstra() { priority_queue<P,vector<P>,greater<P> > q; for(int i=1;i<=n;i++) {d[i]=INF;p[i].reset();} for(int i=1;i<=s;i++) //初始時多個起點,並賦值好bitset的狀態 { q.push(P{0,a[i]}); p[a[i]][i]=1; d[a[i]]=0
; } while (!q.empty()) { int w=q.top().first; int j=q.top().second; q.pop(); if (w>d[j]) continue; for(int k=0;k<g[j].size();k++) { int y=g[j][k].y; int dist=w+g[j][k].w; if (d[y]>dist) { d[y]=dist; p[y]=p[j]; //如果是大於,那麼直接賦值覆蓋原來的
q.push(P(d[y],y)); } else if (d[y]==dist) p[y]|=p[j]; //如果相等,那麼用或來繼承狀態 } } } int main() { while(~scanf("%d%d%d%d",&n,&m,&s,&q)) { memset(g,0,sizeof(g)); for(int i=1;i<=m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); g[u].push_back(Edge{v,w}); g[v].push_back(Edge{u,w}); } for(int i=1;i<=s;i++) scanf("%d",&a[i]); sort(a+1,a+1+s); //要求關鍵點升序輸出 dijkstra(); while(q--) { int x,tot=0; scanf("%d",&x); for(int i=1;i<=s;i++) if (p[x][i]) ans[++tot]=a[i]; //判斷對應位置是否能以最短路到達 for(int i=1;i<tot;i++) printf("%d ",ans[i]); printf("%d\n",ans[tot]); } } return 0; }
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>

#include <time.h>
#include <limits.h>
#include <assert.h>

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
#include <vector>
using namespace std;
#define pb push_back
#define vi vector<int>
#define LL long long
#define pi pair<int,int>
#define MEM(a) memset(a,0,sizeof(a));
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define lowbit(x) ((x)&-(x))
#define SI(a) ((a).size())

#define MAX 25
#define INF (0x3f3f3f3f)
#define F (1000000007)
int n, maxans, minans;
priority_queue <pi> que;
int main(int argc, char const *argv[])
{
	// freopen("data.in", "r", stdin);
	while (~scanf("%d", &n)) {
		while (!que.empty()) que.pop();
		int tot = 0 , sum = 0;
		Rep(i, n) {
			int score; scanf("%d", &score);
			que.push(make_pair(score, 0));
			sum += score;
		}

		minans = INF ,maxans = 0, tot = 0;
		while (1) {
			pi x = que.top();que.pop();
			if (sum % 2 == 0 && 2 * x.first <= sum) {
				maxans = max(maxans,tot+sum/2);
				minans = min(minans,tot+sum/2);
			}
			if(x.first < 3) break;
			 que.push(make_pair(x.first-3,x.second+1));
			 tot++;
			 sum-=3;
		}
		printf("%d %d\n", minans, maxans);
	}
	return 0;
}
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<map>
#define LL long long
#define N 500010
using namespace std;

vector<int> g[N];
map<int,int> mp;
int n,m,val[N];
LL res,ans;
bool v[N];

void dfs(int x,int &edge,int &vertex,int &bi)
{
    v[x]=1;
    edge+=g[x].size()-1;
    vertex++;bi=max(bi,val[x]);
    res+=(LL)val[x]*(g[x].size()-1);
    for(int i=0;i<g[x].size();i++)
        if (!v[g[x][i]]) dfs(g[x][i],edge,vertex,bi);
}

int main()
{
    while(~scanf("%d",&n))
    {
        ans=m=0; mp.clear();
        memset(v,0,sizeof(v));
        memset(g,0,sizeof(g));
        for(int i=1;i<=n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            if (!mp[u]) mp[u]=++m,val[m]=u;
            if (!mp[v]) mp[v]=++m,val[m]=v;
            g[mp[u]].push_back(mp[v]);
            g[mp[v]].push_back(mp[u]);
        }
        for(int i=1;i<=m;i++)
            if (!v[i])
            {
                res=0; int edge,vertex,bi;
                dfs(i,edge=1,vertex=0,bi=0);
                if (edge<vertex) res+=(LL)bi; ans+=res;
            }
        printf("%lld\n",ans);
    }
    return 0;
}