码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5385(The path-构造最短路树)

时间:2015-08-28 21:33:33      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:

The path

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 724    Accepted Submission(s): 277
Special Judge


Problem Description
You have a connected directed graph.Let d(x)技术分享 be the length of the shortest path from 1技术分享 to x技术分享.Specially d(1)=0技术分享.A graph is good if there exist x技术分享 satisfy d(1)<d(2)<....d(x)>d(x+1)>...d(n)技术分享.Now you need to set the length of every edge satisfy that the graph is good.Specially,if d(1)<d(2)<..d(n)技术分享,the graph is good too.

The length of one edge must 技术分享 [1,n]技术分享

It‘s guaranteed that there exists solution.
 

Input
There are multiple test cases. The first line of input contains an integer T技术分享, indicating the number of test cases. For each test case:
The first line contains two integers n and m,the number of vertexs and the number of edges.Next m lines contain two integers each, u技术分享i技术分享技术分享 and v技术分享i技术分享技术分享 (1u技术分享i技术分享,v技术分享i技术分享n)技术分享, indicating there is a link between nodes u技术分享i技术分享技术分享 and v技术分享i技术分享技术分享 and the direction is from u技术分享i技术分享技术分享 to v技术分享i技术分享技术分享.

n3?10技术分享5技术分享技术分享,m6?10技术分享5技术分享技术分享
1n,m10技术分享5技术分享技术分享
 

Output
For each test case,print m技术分享 lines.The i-th line includes one integer:the length of edge from u技术分享i技术分享技术分享 to v技术分享i技术分享技术分享
 

Sample Input
2 4 6 1 2 2 4 1 3 1 2 2 2 2 3 4 6 1 2 2 3 1 4 2 1 2 1 2 1
 

Sample Output
1 2 2 1 4 4 1 1 3 4 4 4
 

Author
SXYZ
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:  5421 5420 5419 5418 5417 
 

我们如果知道每一个点的d[i] 显然cost[i,j]=d[j]-d[i]

我们从起点开始,每次从首或尾加一个节点进来,同时保证连通性

注意不能只找一次,因为首序列与尾序列大小无关

1->n->2->n-1->...->x 反例






#include<bits/stdc++.h> 
using namespace std;
#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 Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100000+10)
#define MAXM (100000+10)
#pragma comment(linker, "/STACK:102400000,102400000")  
#define mp make_pair
#define fi first
#define se second
#define pb push_back
typedef long long ll;

int n,m;
int u[MAXM],v[MAXM],ans[MAXM];

vector<int> To[MAXN];

int id[MAXN];
bool b[MAXN];
int main()
{
//	freopen("F.in","r",stdin);
	
	int T; cin>>T;
	while(T--) {
		MEM(u) MEM(v) MEM(ans) 
		
		cin>>n>>m;
		
		For(i,n) To[i].clear();
		
		For(i,m) {
			scanf("%d%d",&u[i],&v[i]);
			To[u[i]].pb(v[i]);
		}
	
		
		MEM(id) int cnt=0;
		MEM(b) b[1]=1;
		int l=1,r=n;
		while(l<=r) {
			if (b[l]) {
				id[l]=++cnt;
				int sz=To[l].size();
				Rep(i,sz) b[To[l][i]]=1;
				++l;
			}
			else if (b[r]) {
				id[r]=++cnt;
				int sz=To[r].size();
				Rep(i,sz) b[To[r][i]]=1;
				--r;
			}
			
			
		}
		
		
		For(i,m)
		{ 
			ans[i]=id[v[i]]-id[u[i]];
			if (ans[i]<=0||ans[i]>n) ans[i]=n;
			printf("%d\n",ans[i]);
		}
	
	} 	
	
	return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 5385(The path-构造最短路树)

标签:

原文地址:http://blog.csdn.net/nike0good/article/details/48057307

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!