0
点赞
收藏
分享

微信扫一扫

天梯赛自主练习1补题 (2016年初赛)

王栩的文字 2022-04-14 阅读 43
c++

题目
得分应该是240,早年的题确实好做。也带点运气成分

1-4 帅到没朋友
题意: 略。
思路:
 模拟即可,可以发现如果在数量>1的地方出现过,就不可能满足条件了。(我个憨憨写了并查集)。
注意: 输出要用%05d,前边补0,这个值5分。。。
时间复杂度: O(input)
代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
#define mem(a,x) memset(a,x,sizeof(a))
#define fir(i,a,b) for(int i=a;i<=b;++i)
const int N = 2e5+10;
int n,m,k,T;
int cnt[N];
bool vis[N];
void solve()
{
	cin>>n;
	fir(i,0,N-1) cnt[i] = 1;
	for(int i=1;i<=n;++i)
	{
		int num; cin>>num;
		int x; 
		if(num==1)
		{
			cin>>x;
		}	
		else
		{
			while(num--) cin>>x,cnt[x] = 0;
		}
	}
	cin>>m;
	vector<int> va;
	while(m--)
	{
		int x; cin>>x;
		if(vis[x]) continue;
		vis[x] = 1;
		if(cnt[x]) va.push_back(x);
	}
    if(va.size())
    {
    	for(int i=0;i<va.size();++i)
    	{
    		if(i) cout<<" ";
    		printf("%05d",va[i]);
    	}
    }
    else cout<<"No one is handsome";
}
signed main(void)
{
	// ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	solve();
	return 0;
}

L3-1 天梯地图
题意: 给定n个点m条边的图,有时间和距离两种边权。求时间花费最少的路径,如果不唯一,则输出最短路(题目保证该路径唯一)。并且求距离最短的路径,如果不唯一,则输出途中经过点最少的路径。
思路: 刚开始题意看错了,没有看到句号,就写歪了。显然先求一下最短路,然后再求一下最快路,然后输出即可。反正挺麻烦的。
时间复杂度: O(mlogm)
代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<complex>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<list>
#include<set>
#include<queue>
#include<stack>
#define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define fir(i,a,b) for(int i=a;i<=b;++i) 
#define mem(a,x) memset(a,x,sizeof(a))
#define p_ priority_queue
// round() 四舍五入 ceil() 向上取整 floor() 向下取整
// lower_bound(a.begin(),a.end(),tmp,greater<ll>()) 第一个小于等于的
// #define int long long //QAQ
using namespace std;
typedef complex<double> CP;
typedef pair<int,int> PII;
typedef long long ll;
// typedef __int128 it;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const int N = 502*502;
const int M = 1e6+10;
const int mod = 1e9+7;
const double eps = 1e-6;
inline int lowbit(int x){ return x&(-x);}
template<typename T>void write(T x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
    {
        write(x/10);
    }
    putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
int n,m,k,T;
//1为距离、2为时间
int h[N],e[N<<1],ne[N<<1],w[N<<1],w2[N<<1],idx = 0;
int st,ed;
void add(int a,int b,int c,int d)
{
	e[idx] = b,w[idx] = c,w2[idx] = d,ne[idx] = h[a],h[a] = idx++;
}
bool vis[N];
int dist[3][N];
int pre[3][N];
int cnt[N]; //顶点个数
int d[N]; //最快路中的最短
void Dij1(int S)
{
	for(int i=0;i<n;++i) dist[1][i] = 0x3f3f3f3f,pre[1][i] = -1,vis[i] = false;
	priority_queue<PII,vector<PII>,greater<PII> >q;
	dist[1][S] = 0;
	cnt[S] = 1;
	q.push({0,S});
	while(q.size())
	{
		auto tmp = q.top(); q.pop();
		int u = tmp.first,dis = tmp.second;
		swap(u,dis);
		if(vis[u]) continue;
		vis[u] = true;
		for(int i=h[u];~i;i=ne[i])
		{
			int j = e[i];
			if(dist[1][j] > dist[1][u] + w[i])
			{
				dist[1][j] = dist[1][u] + w[i];
				cnt[j] = cnt[u] + 1;
				pre[1][j] = u;
				q.push({dist[1][j],j});
			}
			else if(dist[1][j] == dist[1][u] + w[i])
			{
				if(cnt[j] > cnt[u] + 1)
				{
					cnt[j] = cnt[u] + 1;
					pre[1][j] = u;
				}
			}
		}
	}
}
void Dij2(int S)
{
	for(int i=0;i<n;++i) d[i] = 0x3f3f3f3f,dist[2][i] = 0x3f3f3f3f,pre[2][i] = -1,vis[i] = false;
	priority_queue<PII,vector<PII>,greater<PII> >q;
	dist[2][S] = 0;
	d[S] = 0;
	q.push({0,S});
	while(q.size())
	{
		auto tmp = q.top(); q.pop();
		int u = tmp.first,dis = tmp.second;
		swap(u,dis);
		if(vis[u]) continue;
		vis[u] = true;
		for(int i=h[u];~i;i=ne[i])
		{
			int j = e[i];
			if(dist[2][j] > dist[2][u] + w2[i])
			{
				dist[2][j] = dist[2][u] + w2[i];
				pre[2][j] = u;
				d[j] = d[u] + w[i];
				q.push({dist[2][j],j});
			}
			else if(dist[2][j] == dist[2][u] + w2[i])
			{
				if(d[j] > d[u] + w[i])
				{
					d[j] = d[u] + w[i];
					pre[2][j] = u;
				}
			}
		}
	}
}
void solve()
{
   mem(h,-1); idx = 0;
   // ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
   scanf("%d%d",&n,&m);
   for(int i=0;i<m;++i)
   {
   	  int x,y,op,z,z2;
   	  scanf("%d%d%d%d%d",&x,&y,&op,&z,&z2);
   	  add(x,y,z,z2);
   	  if(op==0) add(y,x,z,z2);
   }
   scanf("%d%d",&st,&ed);
   Dij1(st);
   Dij2(st);
   // fir(i,0,n-1) cout<<i<<":"<<dist[1][i]<<endl;
   stack<int> sa,sb; //时间、距离
   int x = ed;
   while(x != -1)
   {
   	  sa.push(x);
   	  x = pre[2][x];
   }
   x = ed; while(x != -1) {sb.push(x); x = pre[1][x];}
   if(sa == sb)
   {
   	  printf("Time = %d; Distance = %d: ",dist[2][ed],dist[1][ed]);
   	  int t = sa.top(); sa.pop();
   	  printf("%d",t);
   	  while(sa.size())
   	  {
   	  	 t = sa.top(); sa.pop();
   	  	 printf(" => ");
   	  	 printf("%d",t);
   	  }
   }
   else
   {
   	  printf("Time = %d: ",dist[2][ed]);
   	  int t;
   	  t = sa.top(); sa.pop();
   	  printf("%d",t);
   	  while(sa.size())
   	  {
   	  	 t = sa.top(); sa.pop();
   	  	 printf(" => ");
   	  	 printf("%d",t);
   	  }
   	  printf("\n");
   	  printf("Distance = %d: ",dist[1][ed]);
   	  t = sb.top(); sb.pop();
   	  printf("%d",t);
   	  while(sb.size())
   	  {
   	  	 t = sb.top(); sb.pop();
   	  	 printf(" => ");
   	  	 printf("%d",t);
   	  }
   }
}
signed main(void)
{  
   T = 1;
   // OldTomato; cin>>T;
   // read(T);
   while(T--)
   {
   	 solve();
   }
   return 0;
}

L3-3 长城
题意: 略。
思路:
时间复杂度:
代码:

举报

相关推荐

0 条评论