Poj Going Home, pojgoinghome

Source: Internet
Author: User

Poj Going Home, pojgoinghome

Going Home

 

Question:

Give an N * M diagram. m represents people, H represents a house, and each house can only have one person. ask all of you to take the least steps in the house. M is the same as H.

 

Algorithm analysis:

This question is still bare. You can think of finding the distance from each person to each house first. Then find the minimum cost, which seems to be the minimum cost flow? When I started using KM, I found that .... What's wrong? Later I realized that the original question was the minimum cost for solving the problem, and KM was the maximum weight for the largest matching .....

 

# Include <iostream> # include <algorithm> # include <vector> # include <queue> # include <map> # include <cstdio> # include <cstring> using namespace std; typedef pair <int, int> P; const int INF = 1 <20; const int MAXN = 400 + 10; //// // minimum fee struct Edge {int, cap, cost, rev; Edge () {}; Edge (int _ to, int _ cap, int _ cost, int _ rev): to (_ ), cap (_ cap), cost (_ cost), rev (_ rev) {};}; vector <Edge> G [MAXN]; Int V; int h [MAXN]; int dist [MAXN]; int prevv [MAXN], preve [MAXN]; int src, sink; //////////////////////////////////////// /find the distance from struct Point {int x, y, dis; Point () {}; Point (int _ x, int _ y, int _ dis): x (_ x), y (_ y ), dis (_ dis) {};}; vector <Point> cost; map <int, int> mp; int N, M, cnt1, cnt2; char str [MAXN] [MAXN]; int dirx [5] = {-,}; int diry [5] = }; int W [MAXN] [MAXN], ans [MAXN]; int slack, Lx [MAXN], Ly [MAXN], Link [MAXN]; bool S [MAXN], T [MAXN]; /// // bool check (int x, int y) {if (x> = 0 & x <N & y> = 0 & y <M) return true; return false;} void bfs (int s, int e) {bool vst [MAXN] [MAXN]; memset (vst, 0, sizeof (vst); queue <Point> que; que. push (Point (s, e, 0); vst [s] [e] = 1; while (! Que. empty () {Point pv = que. front (); que. pop (); for (int I = 0; I <4; ++ I) {Point tmp; tmp. x = pv. x + dirx [I]; tmp. y = pv. y + diry [I]; tmp. dis = pv. dis + 1; if (check (tmp. x, tmp. y )&&! Vst [tmp. x] [tmp. y]) {vst [tmp. x] [tmp. y] = 1; if (str [tmp. x] [tmp. y] = 'H') {int num = tmp. x * N + tmp. y; if (mp. count (num) = 0) {mp [num] = ++ cnt2;} cost. push_back (Point (cnt1, mp [num], tmp. dis);} que. push (tmp) ;}}/// bool Match (int I) {// S [I] = true; // for (int j = 1; j <= cnt2; ++ j) if (! T [j]) {// int tmp = Lx [I] + Ly [j]-W [I] [j]; // if (tmp = 0) {// T [j] = true; // if (Link [j] =-1 | Match (Link [j]) {// Link [j] = I; // return true; //} // else if (tmp <slack) // slack = tmp; ///} // return false; // void update (int d) {// for (int I = 1; I <= cnt1; ++ I) // if (S [I]) Lx [I]-= d; // for (int I = 1; I <= cnt2; ++ I) // if (T [I]) Ly [I] + = d; //} // bool EK () {// for (int I = 1; I <= cnt1; ++ I) {// Link [I] =-1; // Lx [I] = Ly [I] = 0; // for (int j = 1; j <= cnt2; ++ j) // Lx [I] = max (Lx [I], W [I] [j]); //} // slack = INF; //// for (int I = 1; I <= cnt1; ++ I) {// for (;) {// memset (S, false, sizeof (S); // memset (T, false, sizeof (T); // if (Match (I) // break; /// if (slack = INF) // return false; // update (slack); //} // return true; ///} // int getAns () {// int sum = 0; // for (int j = 1; j <= cnt2; ++ j) {// if (Link [j]! =-1) // sum + = W [Link [j] [j]; //} // return sum; //} void addEdge (int from, int, int cap, int cost) {G [from]. push_back (Edge (to, cap, cost, G [to]. size (); G [to]. push_back (Edge (from, 0,-cost, G [from]. size ()-1);} int min_cost_flow (int s, int t, int f) {int res = 0; V = sink + 1; fill (h, h + V, 0); while (f> 0) {priority_queue <P, vector <P>, greater <P> que; fill (dist, dist + V, INF); dist [s] = 0; que. push (P (0, s )); While (! Que. empty () {P p = que. top (); que. pop (); int v = p. second; if (dist [v] <p. first) continue; for (int I = 0; I <(int) G [v]. size (); ++ I) {Edge & e = G [v] [I]; int tmp = dist [v] + e. cost + h [v]-h [e. to]; if (e. cap> 0 & dist [e. to]> tmp) {dist [e. to] = tmp; prevv [e. to] = v; preve [e. to] = I; que. push (P (dist [e. to], e. to) ;}}}if (dist [t] = INF) {return-1 ;}for (int v = 1; v <V; ++ v) h [v] + = dist [v]; int dd = F; for (int v = t; v! = S; v = prevv [v]) {dd = min (dd, G [prevv [v] [preve [v]. cap);} f-= dd; res + = dd * h [t]; for (int v = t; v! = S; v = prevv [v]) {Edge & e = G [prevv [v] [preve [v]; e. cap-= dd; G [v] [e. rev]. cap + = dd ;}} return res;} void init () {for (int I = 0; I <= sink; ++ I) G [I]. clear () ;}void solve () {cnt1 = 0; cnt2 = 0; mp. clear (); cost. clear (); // search for the distance for (int I = 0; I <N; ++ I) {for (int j = 0; j <M; ++ j) {if (str [I] [j] = 'M') {cnt1 ++; bfs (I, j) ;}} Point tmp; for (int I = 0; I <(int) cost. size (); ++ I) {tmp = cost [I]; W [tmp. x] [Tmp. y] = tmp. dis;} // figure V = cnt1 + cnt2; src = V + 1; sink = src + 1; init (); for (int I = 1; I <= cnt1; ++ I) {// m for (int j = 1; j <= cnt2; ++ j) {// H addEdge (I, j + cnt1, 1, W [I] [j]) ;}}for (int I = 1; I <= cnt1; ++ I) {addEdge (src, I, 1, 0 );} for (int I = 1; I <= cnt2; ++ I ){//!!!!!!!! Pay attention to the changes! AddEdge (cnt1 + I, sink, 1, 0);} int res = min_cost_flow (src, sink, cnt1); printf ("% d \ n", res );} int main () {// freopen ("Input.txt", "r", stdin); while (scanf ("% d", & N, & M ), (N | M) {for (int I = 0; I <N; ++ I) {scanf ("% s", str [I]);} solve ();} return 0 ;}


 


Going home (angel wing Choir) lyrics

Comparison between Chinese and English ~~~ Pai_^

Going Home

Going home, going home
Go home, go home
I am going home
I'm going home
Quiet like, some still day
Calm down like some sleeping days
I am going home
I'm going home

It's not far, just close
Not far away, just in proximity
Through an open door
Crossing an open door
Work all done, care laid
My work has been completed and I will put my thoughts on hold
Never fear no more
No worries

Mother's there expecting me
My mother is looking forward to me.
Father's waiting, too
My father is waiting.
Lots of faces gathered there
Many folks gathered there
All the friends I knew
And all my friends

I'm just going home
I'm going home

No more fear No more pain
No fear, no pain
No more stumbling on the way
No longer on the way.
No more longing for the day
No longer waiting, waiting for time,
Going to run no more
Don't rush

Morning star lights the way
Morning stars light the way back
Restless dreams all gone
Endless dreams have ended
Shadows gone, break of day
Darkness fades and Dawn is approaching
Real life has begun
Real life is coming soon

There's no break, there's no end
No termination, not end
Just living on
Life will continue
Wide awake with a smile
Awake with a smile
Going on and on
Without looking back

Going home, going home
Go home, go home
I am going home
I'm going home
Shadows gone, break of day
Darkness fades and Dawn is approaching
Real life has begun
Real life is coming soon

I'm just going home
I'm going home

Chinese Translation of sophie zelmani-going home lyrics? It is better to correspond to each sentence

Not very often have we met.
But the music's been too bad, But the music has become too bad
Can only sense happiness Can only feel happy
If the music is sad
So, I'm going home So I will go home
I must hurry home I must go home soon
Where a life goes on Where, living continues
We're not kids, so We can't do what We want.
Dreams will keep me young dream will keep me young
Old enough to stress we are not children, can withstand pressure
Only mirrors tell the time Only the mirror is the witness of the years
So, I'm going home So I will go home
I must hurry home I must go home soon
So will my life go on my life will continue
Yes, I'm going home. Yes, I will go home.
Going home alone go home alone
And your life goes on your life continues
So, I'm going home So I will go home
I must hurry home I must go home soon
So will my life go on my life will continue
Yes, I'm going home. Yes, I will go home.
Going home alone go home alone
And your life goes on your life continues
Reference: baike.baidu.com/view/1409447.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.