HDU 4294 spfa

題目:

Groups

After the regional contest, all the ACMers are walking alone a very long avenue to the dining hall in groups. Groups can vary in size for kinds of reasons, which means, several players could walk together, forming a group.
  As the leader of the volunteers, you want to know where each player is. So you call every player on the road, and get the reply like “Well, there are Ai players in front of our group, as well as Bi players are following us.” from the ith player.
  You may assume that only N players walk in their way, and you get N information, one from each player.
  When you collected all the information, you found that you’re provided with wrong information. You would like to figure out, in the best situation, the number of people who provide correct information. By saying “the best situation” we mean as many people as possible are providing correct information.

Input

  There’re several test cases.
  In each test case, the first line contains a single integer N (1 <= N <= 500) denoting the number of players along the avenue. The following N lines specify the players. Each of them contains two integers Ai and Bi (0 <= Ai,Bi < N) separated by single spaces.
  Please process until EOF (End Of File).

Output

  For each test case your program should output a single integer M, the maximum number of players providing correct information.

Sample Input

3
2 0
0 2
2 2
3
2 0
0 2
2 2

Sample Output

2
2
Hint
The third player must be making a mistake, since only 3 plays exist.

Source



思路: 關鍵還是建模,建圖呀~~~ 將每個數對應到一個區間, 將區間映射成一個點,互不相交的區間可以連邊。就拿樣例來說吧, 2 0屬於區間 [3,3],記錄爲點1,0 2屬於區間[1,1] ,記錄爲點2,2 2不能映射到一個合法區間  所以忽略。添加一個起點0,那現在用點0連接所有構造出的點,邊的權值就是 :構造出的點所對應的區間裏出現了幾個人,同樣添加一個終點,所有構造的點向終點連一條邊。權值爲0。  所有的不想交的區間可以連邊。  那現在就是求起點到終點的最長路了,一遍spfa直接水過,具體見代碼吧~~~


#include<stdio.h>   
#include<string.h>   
#include <string>
#include <cmath>
#include <iostream>
#include <map>
#include<vector>   
#include<queue>
#include<algorithm>   
#define fr(i,s,n) for(int i=s;i<n;i++)
#define pf printf
#define sf scanf
#define sfv1(a) scanf("%d",&a)
#define sfv2(n,m) scanf("%d%d",&n,&m)
#define sfv3(u,v,w) scanf("%d%d%d",&u,&v,&w)
#define sfstr(c) scanf("%s",c)
#define pfv1(a) printf("%d\n",a)
#define fi freopen("in.txt","r",stdin)
#define fo freopen("out.txt","w",stdout)
#define cl(a) memset(a,0,sizeof(a))
#define me(a,x) memset(a,x,sizeof(a))
#define inf 2147483647
using namespace std;
typedef long long ll;
int n;

int mp[510][510];
int tot;

struct P{
	int x,y;
	int val;
}p[250020];


const int N=250020;
struct E{
	int u,v,nxt;
}edge[2000010];
int tote,head[N];

void init(){
	tote=0;
	me(head,-1);	
}
inline void addedge(int u,int v){
	edge[tote].u=u;edge[tote].v=v;edge[tote].nxt=head[u];head[u]=tote++;
};
int dis[250020];
int inq[250020];
queue<int> q;

int spfa(){
	int v;
	int cur;
	memset(dis,0,sizeof(int)*(tot+1));
	cl(inq);
	while(!q.empty()) q.pop();
	q.push(0);
	while(!q.empty()){
		cur = q.front();
		q.pop();
		inq[cur]=0;
		for (int i = head[cur]; i != -1; i=edge[i].nxt)  {
			v=edge[i].v;
			if (dis[v]<dis[cur]+p[v].val){
				dis[v]=dis[cur]+p[v].val;
				if (!inq[v]){
					q.push(v);
					inq[v]=1;
				}
			}
		}
	}
	return dis[tot];
}	

int main(){

	int a,b;
	int nn;
	while(sfv1(n)!=EOF){
		init();
		tot=1;
		cl(mp);
		nn=n;
		while(nn--){
			sfv2(a,b);
			if (a+b>=n) continue;
			if (!mp[a][b]) {
				p[tot].x=a+1;
				p[tot].y=n-b;
				p[tot].val=1;
				mp[a][b]=tot++;	
			}else{
				int aim=mp[a][b];
				if (p[aim].val<=p[aim].y-p[aim].x) {
					p[aim].val++;
				}		
			}
		}
		fr(i,1,tot-1){
			fr(j,i+1,tot){
				if (p[i].y<p[j].x){
					addedge(i,j);
				}else if(p[j].y<p[i].x){
					addedge(j,i);
				}
			}
		}
		fr(i,1,tot){
			addedge(0,i);
			addedge(i,tot);
		}
		p[tot].val=0;
		int ans=spfa();
		pfv1(ans);
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章