zoukankan      html  css  js  c++  java
  • 每日算法

    每日算法

    those times when you get up early and you work hard; those times when you stay up late and you work hard; those times when don’t feel like working — you’re too tired, you don’t want to push yourself — but you do it anyway. That is actually the dream. That’s the dream. It’s not the destination, it’s the journey. And if you guys can understand that, what you’ll see happen is that you won’t accomplish your dreams, your dreams won’t come true, something greater will. mamba out


    那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~

    2020.4.2


    luogu-P1195 口袋的天空

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstdlib>
    
    using namespace std;
    const int N = 1005;
    const int M = 100005;
    
    struct node{
    	int a, b ,v;
    };
    int f[N] , n , m , k;
    node arr[M];
    bool vis[N];
    bool cmp(node a,node b){return a.v < b.v;}
    int find(int k)
    {
    	if(f[k] == k)return k;
    	else return f[k] = find(f[k]);
    }
    void kuskal()
    {
    	sort(arr + 1, arr + 1 + m ,cmp);
    	for(int i = 1;i <= n ;i ++)f[i] = i;
    	long long cost = 0, cnt = 0;
    	for(int i = 1;i <= m ;i ++)
    	{
    		int x = find(arr[i].a);
    		int y = find(arr[i].b);
    		if(x != y)
    		{
    			f[x] = y;
    			cost += arr[i].v;
    			cnt++;
    		}
    		if(cnt == n - k)
    		{
    			cout << cost << endl;
    			return;
    		}
    	}
    	cout << "No Answer" << endl;
    }
    int main()
    {
    	cin >> n >> m >> k;
    	for(int i = 1;i <= m ;i ++)
    	{
    		scanf("%d %d %d",&arr[i].a,&arr[i].b,&arr[i].v);
    	}
    	kuskal();
    	return 0;
    }
    

    luogu-P1359 租用游艇

    记忆搜 也可以最短路 / dp

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    const int N = 205;
    const int INF = 0x3f3f3f3f;
    int n;
    int a[N][N];
    int f[N];
    int dfs(int u)
    { 
    	if(u == n)return 0;
    	if(f[u] != -1)return f[u];
    	int cost = INF;
    	for(int i = u + 1;i <= n ;i ++)
    	{
    		cost = min(cost,dfs(i) + a[u][i]);
    	}
    	return f[u] = cost;
    }
    int main()
    {
    	cin >> n;
    	int x = 0;fill(f, f + N , -1);
    	for(int i = 1;i <= n - 1;i ++)
    	{
    		for(int j = i + 1; j <= n ;j ++)
    		{
    			scanf("%d",&x);
    			a[i][j] = x;
    		}
    	}
    	cout << dfs(1) << endl;
    	return 0;
    }
    

    luogu-P1158 导弹拦截

    贪心

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    
    using namespace std;
    const int N = 100005;
    int x1,y1,x2,y2, n;
    int dis(int sx,int sy,int tx,int ty)
    {
    	return (sx-tx)*(sx-tx) + (sy-ty)*(sy-ty);
    }
    struct node{
    	int a , b;
    	int x , y;
    }arr[N];
    bool cmp(node a,node b){
    	if(a.a == b.a)return a.b < b.b;
    	else return a.a < b.a;
    }
    int main()
    {
    	cin >> x1 >> y1 >> x2 >> y2;
    	cin >> n;
    	for (int i = 0; i < n; ++i)
    	{
    		scanf("%d %d",&arr[i].x,&arr[i].y);
    		arr[i].a = dis(x1,y1,arr[i].x,arr[i].y);
    		arr[i].b = dis(x2,y2,arr[i].x,arr[i].y);
    	}
    	sort(arr, arr + n , cmp);
    	int a = 0, ans = arr[n-1].a;
    	for(int i = n - 2;i >= 0 ;i --)
    	{
    		a = max(a , arr[i + 1].b);
    		ans = min(ans , a + arr[i].a);
    	}
    	cout << ans << endl;
    	return 0;
    }
    

    dijkstra模板

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstdlib>
    #include <queue>
    
    using namespace std;
    const int N = 100010;
    const int M = 500010;
    
    struct edge
    {
    	int to, dis , next;
    }e[M];
    int head[N], dis[N], cnt, n , m ,s;
    bool vis[N];
    
    void add(int a,int b,int v)
    {
    	cnt++;
    	e[cnt].dis = v;
    	e[cnt].to = b;
    	e[cnt].next = head[a];
    	head[a] = cnt;
    }
    struct node
    {
    	int dis;
    	int pos;
    	bool operator <(const node &x)const{
    		return x.dis < dis;
    	}
    };
    
    priority_queue<node> q;
    
    
    void dijkstra()
    {
    	dis[s] = 0;
    	q.push((node){0,s});
    	while(!q.empty())
    	{
    		node tmp = q.top();
    		q.pop();
    		int x = tmp.pos, d = tmp.dis;
    		if(vis[x])continue;
    		vis[x] = 1;
    		for(int i = head[x];i  ;i = e[i].next)
    		{
    			int y = e[i].to;
    			if(dis[y] > dis[x] + e[i].dis)
    			{
    				dis[y] = dis[x] + e[i].dis;
    				if(!vis[y])
    				{
    					q.push((node){dis[y],y});
    				}
    			}
    		}
    	}
    }
    int main()
    {
    	cin >> n >> m >> s;
    	for(int i = 1;i <= n ;i ++)dis[i] = 0x7fffffff;
    	for(register int i = 0;i < m ; ++i)
    	{
    		register int a , b , c;
    		scanf("%d %d %d",&a , &b ,&c);
    		add(a , b ,c);
    	}
    	dijkstra();
    	for(int i = 1;i <= n ;i ++)
    	{
    		printf("%d ", dis[i]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    005.SQLServer AlwaysOn可用性组高可用简介
    004.Windows Server 故障转移群集 (WSFC)简介
    003.SQLServer数据库镜像高可用部署
    附008.Kubernetes TLS证书介绍及创建
    附007.Kubernetes ABAC授权
    附006.Kubernetes RBAC授权
    附005.Kubernetes身份认证
    附004.Kubernetes Dashboard简介及使用
    附003.Kubeadm部署Kubernetes
    附002.Minikube介绍及使用
  • 原文地址:https://www.cnblogs.com/wlw-x/p/12629686.html
Copyright © 2011-2022 走看看