HDU 5385(The path-構造最短路樹),hdupath-
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 DescriptionYou have a connected directed graph.Let d(x) be the length of the shortest path from 1 to x.Speciallyd(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,ifd(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.
InputThere are multiple test cases. The first line of input contains an integerT, 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,ui and vi(1≤ui,vi≤n), indicating there is a link between nodes ui and vi and the direction is from ui to vi.
∑n≤3∗105,∑m≤6∗105
1≤n,m≤105
OutputFor each test case,print m lines.The i-th line includes one integer:the length of edge from ui to vi
Sample Input
24 61 22 41 31 22 22 34 61 22 31 42 12 12 1
Sample Output
122144113444
AuthorSXYZ
Source2015 Multi-University Training Contest 8
Recommendwange2014 | 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_backtypedef 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;}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。