标签:rom direct smi graph head direction contain otherwise std
input | output |
---|---|
4 10 2 3 0 20 1 0 40 1 4 0 100 3 0 1 6 |
6/1 -3/1 -3/2 3/5 |
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <queue> #include <vector> #define inf 0x3f3f3f3f #define met(a,b) memset(a,b,sizeof a) #define pb push_back typedef long long ll; using namespace std; const int N = 1e3+10; const int M = 124750+10; const int mod=1e9+7; int n,m,k,tot=0,s,t,v; int head[N],vis[N],speed[N],sum[N]; struct ANS{int f,s;}ans[N]; struct EDG{int to,next;}edg[N*N]; void add(int u,int v){ edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++; } int gcd(int x,int y){ int xx=x,yy=y; x=max(xx,yy); y=min(yy,xx); if(x%y==0)return y; return gcd(x-y,y); } void bfs(){ queue<int>q; q.push(s);vis[s]=1;ans[s].f=t;ans[s].s=1; while(!q.empty()){ int u=q.front();q.pop();//printf("!!!%d %d %d\n",u,ans[u].f,ans[u].s);system("pause"); for(int i=head[u];i!=-1;i=edg[i].next){ int v=edg[i].to; if(!vis[v]){ vis[v]=1; int g=gcd(abs(sum[u]*ans[u].f),sum[v]*ans[u].s); ans[v].f=-sum[u]*ans[u].f/g;ans[v].s=sum[v]*ans[u].s/g; q.push(v); } } } } int main() { met(ans,0);met(head,-1); int u,v; scanf("%d",&n); for(int u=1;u<=n;u++){ scanf("%d",&sum[u]); while(~scanf("%d",&v)&&v){ add(u,v);add(v,u); } } scanf("%d%d",&s,&t); bfs(); for(int i=1;i<=n;i++){ if(ans[i].f>0){ printf("%d/%d\n",ans[i].f,ans[i].s); }else if(ans[i].f==0){ printf("0/1\n"); }else { printf("-%d/%d\n",-ans[i].f,ans[i].s); } } return 0; }
标签:rom direct smi graph head direction contain otherwise std
原文地址:http://www.cnblogs.com/jianrenfang/p/6009511.html