从零单排13

图论完全木有入门的说。。。

看到什么差分约束直接就蒙了。。

最短路一直在套模版还没有领会要义。。

上午熟悉了优先队列。。

做了四题。。。

昨天练了十几道搜索

今天如果没有其他的事

可以做做动态规划


hdu 1509:http://acm.hdu.edu.cn/showproblem.php?pid=1509

/*
优先队列+STL模版题
注意排序稳定性
*/
#include<iostream>
#include<queue>
using namespace std;
struct node
{
	char name[50];
	int parameter;
	int priority;
	int order;
	bool operator<(const node &x)const
	{
		if(priority==x.priority)
		{
			return order>x.order;
		}
		else
		{
			return priority>x.priority;
		}
	}
};
node temp;
priority_queue<node>q;
int main()
{
	int i=0;
	char str[5];
	while(cin>>str)
	{
		if(strcmp(str,"GET")==0)
		{
			if(!q.empty())
			{
				temp=q.top();
				cout<<temp.name<<" "<<temp.parameter<<endl;
				q.pop();
			}
			else
			{
				cout<<"EMPTY QUEUE!"<<endl;
			}
		}
		else
		{
			cin>>temp.name>>temp.parameter>>temp.priority;
			temp.order=++i;
			q.push(temp);
		}
	}
	system("pause");
	return 0;
}

hdu 1873: http://acm.hdu.edu.cn/showproblem.php?pid=1873

/*
优先队列+STL模版题。。
跟上题不同的是要开3个优先队列。。。
*/
#include<iostream>
#include<queue>
using namespace std;
struct node
{
	int priority;
	int order;
	bool operator<(const node &x)const
	{
		if(priority==x.priority)
		{
			return order>x.order;
		}
		else
		{
			return priority<x.priority;
		}
	}
};
node temp;
priority_queue<node>q1,q2,q3;
int main()
{
	char str[5];
	int n;
	while(cin>>n)
	{
		int k=1;
		while(!q1.empty())
		{
			q1.pop();
		}
		while(!q2.empty())
		{
			q2.pop();
		}
		while(!q3.empty())
		{
			q3.pop();
		}
		for(int i=1;i<=n;i++)
		{
			cin>>str;
			if(strcmp(str,"IN")==0)
			{
				int x,y;
				cin>>x>>y;
				temp.priority=y;
				temp.order=k++;
				if(x==1)
				{
					q1.push(temp);
				}
				else if(x==2)
				{
					q2.push(temp);
				}
				else
				{
					q3.push(temp);
				}
			}
			else
			{
				int x;
				cin>>x;
				if(x==1)
				{
					if(q1.empty())
					{
						cout<<"EMPTY"<<endl;
					}
					else
					{
						temp=q1.top();
						cout<<temp.order<<endl;
						q1.pop();
					}
				}
				else if(x==2)
				{
					if(q2.empty())
					{
						cout<<"EMPTY"<<endl;
					}
					else
					{
						temp=q2.top();
						cout<<temp.order<<endl;
						q2.pop();
					}
				}
				else
				{
					if(q3.empty())
					{
						cout<<"EMPTY"<<endl;
					}
					else
					{
						temp=q3.top();
						cout<<temp.order<<endl;
						q3.pop();
					}
				}
			}
		}
	}
	system("pause");
	return 0;
}
	

hdu 4006: http://acm.hdu.edu.cn/showproblem.php?pid=4006

/*
忘记清空队列wa了一次
第二种优先队列的构造方式
优先队列默认是从大到小排列,如果想按从小到大排列
需要priority_queue<int,vector<int>,greater<int> >q;
同时维护一个size=k的队列即可 
*/
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
priority_queue<int ,vector<int>,greater<int> >q;
int main()
{
	int n,k;
	while(cin>>n>>k)
	{
		while(!q.empty())
		{
			q.pop();
		}
		while(n--)
		{
			char str[5];
			cin>>str;
			if(strcmp(str,"I")==0)
			{
				int num;;
				cin>>num;
				q.push(num);
				if(q.size()>k)
				{
					q.pop();
				}
			}
			else
			{
				cout<<q.top()<<endl;
			}
		}
	}
	system("pause");
	return 0;
}
		
				

hdu 1896: http://acm.hdu.edu.cn/showproblem.php?pid=1896

/*
优先队列模版~
*/
#include<iostream>
#include<queue>
using namespace std;
struct node
{
	int p;
	int d;
	bool operator<(const node &x) const
	{
		if(p==x.p)
			return d>x.d;
		else 
			return p>x.p;
	}
};
node temp;
priority_queue<node> q;
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		cin>>n;
		while(!q.empty())
		{
			q.pop();
		}
		while(n--)
		{
			cin>>temp.p>>temp.d;
			q.push(temp);
		}
		int k=1;
		while(!q.empty())
		{
			temp=q.top();
			q.pop();
			if(k%2==1)
			{
				temp.p+=temp.d;
				q.push(temp);
			}
			k++;
		}
		cout<<temp.p<<endl;
	}
	system("pause");
	return 0;
}

hdu 2544: http://acm.hdu.edu.cn/showproblem.php?pid=2544

/*
采用优先队列优化dijkstra,
同时采用pair和make_pair使代码更简练 
学到了好多东西~!
*/
#include<iostream>
#include <queue>
#define MAXN 105
#define INF 0x7ffffff
using namespace std;
int map[MAXN][MAXN];
bool visit[MAXN];
int d[MAXN];
int N,M;
int dijkstra(int u)
{
 	for(int i=1;i<=N;i++)
  		d[i]=INF;
 	d[u]=0;
 	priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q; 
 	q.push(make_pair(d[u],u));
 	while(!q.empty())
 	{
  		pair<int,int> t=q.top();
  		q.pop();
  		int now=t.second;
  		if(visit[now])
   			continue;
  		visit[now]=true;
  		for(int j=1;j<=N;j++)
  		{
   			if(!visit[j]&&map[now][j]<INF&&d[j]>d[now]+map[now][j])
   			{
   				d[j]=d[now]+map[now][j];
   	 			q.push(make_pair(d[j],j));
   			}
		}
 	}
 	return d[N];
}

int main()
{
 	while(cin>>N>>M,N!=0&&M!=0)
	{
  		for(int i=1;i<=N;i++)
  		{
   			for(int j=1;j<=N;j++)
   			{
    			if(i==j)
     				map[i][j]=0;
    			else
     				map[i][j]=INF;
   			}
		}
  		for(int i=1;i<=M;i++)
  		{
			int x,y,time;
   			cin>>x>>y>>time;
   			if(time<map[x][y])
    			map[x][y]=map[y][x]=time;
  		}
  		memset(d,0,sizeof(d));
  		memset(visit,0,sizeof(visit));
  		cout<<dijkstra(1)<<endl;
 	}
 	system("pause");
 	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值