'공부/알고리즘2'에 해당되는 글 13건

  1. 2014.12.04 Longest Common Subsequence
  2. 2014.12.04 Parallel Sort 1
  3. 2014.11.27 토너먼트 노멀 케이스 (n != 2^k)
  4. 2014.11.20 Tournament : Bin Packing Problem
  5. 2014.11.13 HIGHEST TOWER BF + TOPOL + GRAPH
  6. 2014.11.13 Topological Sort + Bellman Ford로 O(e) 시간에 풀기
  7. 2014.11.06 RACE bellman Ford
  8. 2014.11.06 maxflow bipar
  9. 2014.11.06 maxflow 탈출문제
  10. 2014.10.16 bfs
posted by pflower 2014. 12. 4. 11:29


Longest Common Subsequence.cpp


#include <iostream>

#include <fstream>

#include <math.h>

#include <string>



#define UP 0

#define LEFT 1

#define MATCH 2


using namespace std;


int mat[100][100];

int c[100][100];

int b[100][100];

std::string x;

std::string y;

void lenlen()

{

//int &ref = cache[]

for(int i = 0; i <= x.length(); i ++)

{

c[i][0] = 0;

}

for(int i = 0; i <= y.length(); i ++)

{

c[0][i] = 0;

}


for(int i = 1; i <= x.length(); i ++)

{

for(int j = 1; j <= y.length(); j ++)

{

if(x[i - 1] == y[j - 1])

{

c[i][j] = c[i - 1][j - 1] + 1;

b[i][j] = MATCH;

}

else if(c[i - 1][j] > c[i][j - 1])

{

c[i][j] = c[i - 1][j];

b[i][j] = UP;

}

else

{

c[i][j] = c[i][j - 1];

b[i][j] = LEFT;

}

}

}

}



void print(int i, int j)

{

if(i == 0 && j == 0)

return;

if(i < 0 || j < 0)

return;

if(b[i][j] == MATCH)

{

print(i - 1, j - 1);

cout << x[i - 1];

}

else if(b[i][j] == UP)

{

print(i - 1, j);

}

else

print(i, j - 1);

}


int main()

{

fstream fin("input.txt");


int n, m;

fin >> n >> m;

fin >> x >> y;

lenlen();

cout << c[n][m] << endl;

print(n, m);

}

'공부 > 알고리즘2' 카테고리의 다른 글

Parallel Sort  (1) 2014.12.04
토너먼트 노멀 케이스 (n != 2^k)  (0) 2014.11.27
Tournament : Bin Packing Problem  (0) 2014.11.20
HIGHEST TOWER BF + TOPOL + GRAPH  (0) 2014.11.13
Topological Sort + Bellman Ford로 O(e) 시간에 풀기  (0) 2014.11.13
posted by pflower 2014. 12. 4. 11:27

#include <iostream>

#include <fstream>

#include <math.h>


using namespace std;


int n;

int mat[100][100];


int main()

{

fstream fin("input.txt");



fin >> n;

for(int i = 0; i < n; i ++)

{

for(int j = 0; j < n; j ++)

fin >> mat[i][j];

}

int v = log((double)n) / log(2.0);

for(int iter = 0; iter < v ; iter++)// 로그엔번

{

for(int i = 0; i < n; i ++)

{

for(int k = 0; k < n; k ++)

{

for(int j = 0; j + 1 < n; j += 2)

{

if( (i % 2) == 0 && mat[i][j] > mat[i][j + 1])

{

std::swap(mat[i][j], mat[i][j + 1]);

}

else if( (i % 2) == 1 && mat[i][j] < mat[i][j + 1])

{

std::swap(mat[i][j], mat[i][j + 1]);

}

}

for(int j = 1; j + 1 < n; j += 2)

{

if( (i % 2) == 0 && mat[i][j] > mat[i][j + 1])

{

std::swap(mat[i][j], mat[i][j + 1]);

}

else if( (i % 2) == 1 && mat[i][j] < mat[i][j + 1])

{

std::swap(mat[i][j], mat[i][j + 1]);

}

}

}

}



cout << endl;

/// 수직 검사

for(int j = 0; j < n; j ++){

for(int k = 0; k < n; k ++)

{

for(int i = 0; i + 1 < n; i += 2) 

{

if( mat[i][j] > mat[i + 1][j])

std::swap(mat[i][j], mat[i + 1][j]);

}


for(int i = 1; i + 1 < n; i += 2) 

{

if( mat[i][j] > mat[i + 1][j])

std::swap(mat[i][j], mat[i + 1][j]);

}

}

}

}


for(int i = 0; i < n; i ++)

{

for(int k = 0; k < n; k ++)

{

for(int j = 0; j + 1 < n; j += 2) 

{

if( mat[i][j] > mat[i][j + 1])

{

std::swap(mat[i][j], mat[i][j + 1]);

}

}


for(int j = 1; j + 1 < n; j += 2) 

{

if( mat[i][j] > mat[i][j + 1])

{

std::swap(mat[i][j], mat[i][j + 1]);

}

}

}

}


for(int i = 0; i < n; i ++)

{

for(int j = 0; j < n; j ++)

cout << mat[i][j] << " ";

cout << endl;

}

}

Parrelel Sort.cpp


posted by pflower 2014. 11. 27. 11:01


checkcheck.cpp

#include <iostream>

#include <fstream>


using namespace std;



int n, m, size;

int tree[1000];

int player[1000];


double Log2( double n )  

{  

    // log(n)/log(2) is log2.  

    return log( n ) / log( 2.0 );  

}


void doBin(int input)

{

int head = 1;

int s = pow(2.0, (ceil(Log2(n) + 0.5) - 1));

int offset = s + s - 1;

int LowExt = 2 * ((n - 1) - (s - 1));


while(head < n) // 찾아가기

{

if(head * 2 >= n)

break;

//if(player[tree[head * 2]] >= player[tree[head * 2 + 1]]) // 왼쪽이 더 크면

if(player[tree[head * 2]] >= input) // 왼쪽이 더 크면

{

head *= 2;

}

else

{

head = head * 2 + 1;

}

}


if(head * 2 > offset) // 최하단 레벨

{

if(player[head * 2 - (offset)] >= input) // 왼쪽이 더 크면

{

player[head * 2 - (offset)] -= input;

//tree[head] = head * 2 - (n - 1); // 왼쪽이 새엄마

}

else

{

player[head * 2 - (offset) + 1] -= input;

//tree[head] = head * 2 - (n - 1) + 1; // 오른쪽이 새엄마

}


// 리프노드 처리

if(player[head * 2 - (offset)] >= player[head * 2 - (offset) + 1]) // 왼쪽이 더 크면

{

tree[head] = head * 2 - (offset); // 왼쪽이 새엄마

}

else

{

tree[head] = head * 2 - (offset) + 1; // 오른쪽이 새엄마

}

}

else // 최하단 레벨 + 1 // k > LowExt

{

if(player[head * 2 - (n - 1) + LowExt] >= input) // 왼쪽이 더 크면

{

player[head * 2 - (n - 1) + LowExt] -= input;

//tree[head] = head * 2 - (n - 1); // 왼쪽이 새엄마

}

else

{

player[head * 2 - (n - 1) + 1 + LowExt] -= input;

//tree[head] = head * 2 - (n - 1) + 1; // 오른쪽이 새엄마

}


// 리프노드 처리

if(player[head * 2 - (n - 1) + LowExt] >= player[head * 2 - (n - 1) + LowExt + 1]) // 왼쪽이 더 크면

{

tree[head] = head * 2 - (n - 1) + LowExt; // 왼쪽이 새엄마

}

else

{

tree[head] = head * 2 - (n - 1) + LowExt + 1; // 오른쪽이 새엄마

}

}


if((n % 2 == 1) && head / 2 == (s - 1 + LowExt / 2) / 2) // 혼재된 구간

{

head = head / 2;

if(player[tree[head * 2]] >= player[head * 2 - (n - 1) + LowExt + 1]) // 왼쪽이 더 크면

{

tree[head] = tree[head * 2]; // 왼쪽이 새엄마

}

else

{

tree[head] = head * 2 - (n - 1) + LowExt + 1; // 오른쪽이 새엄마

}

}


head = head / 2;


while(head > 0) // 트리 올라가면서 복구

{

if(player[tree[head * 2]] >= player[tree[head * 2 + 1]]) // 왼쪽이 더 크면

{

tree[head] = tree[head * 2]; // 왼쪽이 새엄마

}

else

{

tree[head] = tree[head * 2 + 1]; // 오른쪽이 새엄마

}


head = head / 2;

}


for(int i = 1; i <= n; i ++)

{

cout << player[i] << " ";

}


cout << endl;

}


void init()

{

int p = -1;

int s = pow(2.0, (ceil(Log2(n) + 0.5) - 1));

int offset = s + s - 1;

int LowExt = 2 * ((n - 1) - (s - 1));

if (n % 2 == 0)

{

 for (int k=1; k<=n; k=k+2) {  

if(k <= LowExt)

{

p = (k +offset)/2;

}

else

{

p = (k - LowExt + n -1)/2;

}

 tree[p] = k; 

 }

}

else { // n 이 홀수

for (int k = 1; k <= LowExt; k=k+2)

p = (k + offset) / 2;

tree[p] = k;

}

for(int k = LowExt  + 2; k <= n; k=k+2 )

p = (k  - LowExt + n - 1) / 2; 

tree[p] = k;

}

}


for (int k = (n-1)/2; k > 0; k--) 

tree[k] = tree[k * 2];

}


int main()

{

fstream fin("input.txt");


fin >> n >> m >> size;


for(int i = 1; i <= n; i ++)

{

player[i] = size;

}


init();


for(int i = 0; i < m; i ++)

{

int input;

fin >> input;


doBin(input);

}


for(int i = 1; i <= n; i ++)

{

cout << player[i] << " ";

}


cout << endl;


return 0;

}

'공부 > 알고리즘2' 카테고리의 다른 글

Longest Common Subsequence  (0) 2014.12.04
Parallel Sort  (1) 2014.12.04
Tournament : Bin Packing Problem  (0) 2014.11.20
HIGHEST TOWER BF + TOPOL + GRAPH  (0) 2014.11.13
Topological Sort + Bellman Ford로 O(e) 시간에 풀기  (0) 2014.11.13
posted by pflower 2014. 11. 20. 10:37

asdf.cpp


#include <iostream>

#include <fstream>


using namespace std;



int n, m, size;

int tree[1000];

int player[1000];


void doBin(int input)

{

int head = 1;

while(head < n) // 찾아가기

{

if(head * 2 >= n)

break;

//if(player[tree[head * 2]] >= player[tree[head * 2 + 1]]) // 왼쪽이 더 크면

if(player[tree[head * 2]] >= input) // 왼쪽이 더 크면

{

head *= 2;

}

else

{

head = head * 2 + 1;

}

}


if(player[head * 2 - (n - 1)] >= input) // 왼쪽이 더 크면

{

player[head * 2 - (n - 1)] -= input;

//tree[head] = head * 2 - (n - 1); // 왼쪽이 새엄마

}

else

{

player[head * 2 - (n - 1) + 1] -= input;

//tree[head] = head * 2 - (n - 1) + 1; // 오른쪽이 새엄마

}



// 리프노드

if(player[head * 2 - (n - 1)] >= player[head * 2 - (n - 1) + 1]) // 왼쪽이 더 크면

{

tree[head] = head * 2 - (n - 1); // 왼쪽이 새엄마

}

else

{

tree[head] = head * 2 - (n - 1) + 1; // 오른쪽이 새엄마

}


head = head / 2;

while(head > 0) // 올라가면서 복구

{

if(player[tree[head * 2]] >= player[tree[head * 2 + 1]]) // 왼쪽이 더 크면

{

tree[head] = tree[head * 2]; // 왼쪽이 새엄마

}

else

{

tree[head] = tree[head * 2 + 1]; // 오른쪽이 새엄마

}


head = head / 2;

}


for(int i = 1; i <= n; i ++)

{

cout << player[i] << " ";

}


cout << endl;

}


void init()

{

int p = -1;

// 인터널 노드 아래 초기화

for (int k = 1; k <= n; k += 2)

{

p = (k + n - 1) / 2;

tree[p] = k;

}

for(int k = (n - 1) / 2 ; k > 0; k --)

{

tree[k] = tree[k * 2];

}

}


int main()

{

fstream fin("input.txt");


fin >> n >> m >> size;


for(int i = 1; i <= n; i ++)

{

player[i] = size;

}


init();


for(int i = 0; i < m; i ++)

{

int input;

fin >> input;


doBin(input);

}


for(int i = 1; i <= n; i ++)

{

cout << player[i] << " ";

}


cout << endl;


return 0;

}

'공부 > 알고리즘2' 카테고리의 다른 글

Parallel Sort  (1) 2014.12.04
토너먼트 노멀 케이스 (n != 2^k)  (0) 2014.11.27
HIGHEST TOWER BF + TOPOL + GRAPH  (0) 2014.11.13
Topological Sort + Bellman Ford로 O(e) 시간에 풀기  (0) 2014.11.13
RACE bellman Ford  (0) 2014.11.06
posted by pflower 2014. 11. 13. 11:36

#include <iostream>


#include <fstream>




using namespace std;




int v, e;




struct Link


{


Link(int val, int cost)


{


this->val = val;

this->cost = cost;

next = NULL;


}


int val;

int cost;


Link *next;


};




Link* store = NULL;


Link* graph[500];


int seen[500];


int p[1000];

int d[1000];


void dfs(int i)

{


if(seen[i])


return;




seen[i] = true;




for(Link *temp = graph[i]; temp != NULL; temp = temp->next)


{


dfs(temp->val);


}




if(store == NULL)

{

store = new Link(i, 0);

}


else


{


Link *temp = store;

store = new Link(i, 0);

store->next = temp;


}


}




void topological_sort()

{

for(int i = 1; i <= v; i ++)

dfs(i);

}




void bf(int u, int v, int c)

{


   if (d[v] < d[u] + c)

   { 

  d[v] = d[u] + c;

       p[v] = u; 

   }


}



int w[1000];

int h[1000];

int m[1000];


int main()


{


fstream fin("input.txt");




fin >> v;



for(int i = 1; i <= v; i ++)

{

int a, b, c; // 

fin >> a >> b >> c;

w[i] = a;

h[i] = b;

m[i] = c;

}


for(int i = 1; i <= v; i ++)

{

if(graph[0] == NULL)


{


graph[0] = new Link(i, 0);


}


else


{


Link *temp = NULL;


for(temp = graph[0]; temp->next != NULL; temp = temp->next);


temp->next = new Link(i, 0);


}

}


for(int i = 1; i <= v; i ++)

{

for(int j = 1; j <= v; j ++)

{

if(i != j)

{

if(w[i] >= w[j] && m[i] >= m[j])

{

if(graph[i] == NULL)

{


graph[i] = new Link(j, h[i]);


}


else


{


Link *temp = NULL;


for(temp = graph[i]; temp->next != NULL; temp = temp->next);


temp->next = new Link(j, h[i]);


}

}

}

}

}



for(int i = 1; i <= v; i ++)

{

if(graph[i] == NULL)

{


graph[i] = new Link(v + 1, h[i]);


}


else

{


Link *temp = NULL;

for(temp = graph[i]; temp->next != NULL; temp = temp->next);

temp->next = new Link(v + 1, h[i]);

}

}


//for(int i = 0; i < e; i ++)

//{


// int a, b, c;


// fin >> a >> b >> c;


//


//


//}

for(int i = 0; i <= v + 1; i ++) {

d[i] = 0;

}

for(int i = 0; i <= e; i ++) {

p[i] = -1;


}


d[0] = 0;




topological_sort();



for(Link * temp= graph[0]; temp != NULL; temp = temp->next)

{

bf(0, temp->val, temp->cost);

}


for(Link * temp= store; temp != NULL; temp = temp->next)

{


for(Link * head = graph[temp->val]; head != NULL; head = head->next)

{

bf(temp->val, head->val, head->cost);

}

}





int i = v + 1;

while(true) {

i = p[i];

if(i == 0)

break;

cout << i << " ";

}

cout << endl;


cout << "height : " << d[v + 1] << endl;

}

'공부 > 알고리즘2' 카테고리의 다른 글

토너먼트 노멀 케이스 (n != 2^k)  (0) 2014.11.27
Tournament : Bin Packing Problem  (0) 2014.11.20
Topological Sort + Bellman Ford로 O(e) 시간에 풀기  (0) 2014.11.13
RACE bellman Ford  (0) 2014.11.06
maxflow bipar  (0) 2014.11.06
posted by pflower 2014. 11. 13. 09:55

#include <iostream>


#include <fstream>




using namespace std;




int v, e;




struct Link


{


Link(int val, int cost)


{


this->val = val;

this->cost = cost;

next = NULL;


}


int val;

int cost;


Link *next;


};




Link* store = NULL;


Link* graph[500];


int seen[500];


int p[1000];

int d[1000];


void dfs(int i)


{


cout << i << endl;


if(seen[i])


return;




seen[i] = true;




for(Link *temp = graph[i]; temp != NULL; temp = temp->next)


{


dfs(temp->val);


}




if(store == NULL)

{

store = new Link(i, 0);

}


else


{


Link *temp = store;

store = new Link(i, 0);

store->next = temp;


}


}




void topological_sort()

{

for(int i = 1; i <= v; i ++)

dfs(i);

}




void bf(int u, int v, int c)

{


   if (d[v] > d[u] + c)

   { 

  d[v] = d[u] + c;

       p[v] = u; 

   }


}




int main()


{


fstream fin("input.txt");




fin >> v >> e;




for(int i = 0; i < e; i ++)


{


int a, b, c;


fin >> a >> b >> c;



if(graph[a] == NULL)


{


graph[a] = new Link(b, c);


}


else


{


Link *temp = NULL;


for(temp = graph[a]; temp->next != NULL; temp = temp->next);


temp->next = new Link(b, c);


}


}

for(int i = 0; i <= v + 1; i ++) {

d[i] = 987654321;

}

for(int i = 0; i <= e; i ++) {

p[i] = -1;


}


d[0] = 0;





cout << "<--DFS Result-->" << endl;


topological_sort();


cout << "<--Topological Sort Result-->" << endl;


cout << "0" << endl;

for(Link * temp= graph[0]; temp != NULL; temp = temp->next)

{

bf(0, temp->val, temp->cost);

}


for(Link * temp= store; temp != NULL; temp = temp->next)

{

cout << temp->val << endl;


for(Link * head = graph[temp->val]; head != NULL; head = head->next)

{

bf(temp->val, head->val, head->cost);

}

}



cout << "d[i] : " << endl;


for(int i = 0; i <= v + 1; i ++){

cout << d[i] << " ";

}



cout << endl;

}

'공부 > 알고리즘2' 카테고리의 다른 글

Tournament : Bin Packing Problem  (0) 2014.11.20
HIGHEST TOWER BF + TOPOL + GRAPH  (0) 2014.11.13
RACE bellman Ford  (0) 2014.11.06
maxflow bipar  (0) 2014.11.06
maxflow 탈출문제  (0) 2014.11.06
posted by pflower 2014. 11. 6. 11:29

#include <iostream>

#include <fstream>


using namespace std;


fstream fin;


int d[1000];

int p[1000];


int t[1000];

int dist[1000];


int T;

int n;


void bf(int u, int v, int c)

{

   if (d[v] > d[u] + c)

   { 

  d[v] = d[u] + c;

       p[v] = u; 

   }

}


int main()

{

fin.open("input.txt");

fin >> T >> n;


for(int i = 1; i <= n + 1; i ++)

fin >> dist[i];

for(int i = 1; i <= n; i ++)

fin >> t[i];


t[n + 1] = 0;


for(int i = 0; i <= n; i ++) {

d[i] = 987654321;

p[i] = -1;

}

d[0] = 0;

for(int i = 0;  i <= n; i ++) {

int localLen = 0;

for(int j = i + 1; j <= n + 1; j ++) {

localLen += dist[j];

if(localLen > T)

break;

bf(i, j, t[j]);

}

}


int head = n;

int sumtime = 0;

int ans[100];

int iter = 0;


while(head != 0)

{

sumtime += t[head];

ans[iter++] = head;

head = p[head];

}


cout << sumtime << endl;

cout << iter << endl;

for(int i = iter - 1; i >= 0; i --)

{

cout << ans[i] << " ";

}



cout << endl;


return 0;

}

'공부 > 알고리즘2' 카테고리의 다른 글

HIGHEST TOWER BF + TOPOL + GRAPH  (0) 2014.11.13
Topological Sort + Bellman Ford로 O(e) 시간에 풀기  (0) 2014.11.13
maxflow bipar  (0) 2014.11.06
maxflow 탈출문제  (0) 2014.11.06
bfs  (0) 2014.10.16
posted by pflower 2014. 11. 6. 09:58

#include <iostream>

#include <iostream>

#include <fstream>



using namespace std;



#define UP(i, j) flowInput(((i - 1) * n + j) * 2, ((i - 2) * n + j) * 2 - 1, 1); // 위와 연결

#define DOWN(i, j) flowInput(((i - 1) * n + j) * 2, (i * n + j) * 2 - 1, 1); // 아래와 연결

#define LEFT(i, j) flowInput(((i - 1) * n + j) * 2, ((i - 1) * n + (j - 1)) * 2 - 1, 1); // 왼쪽과 연결

#define RIGHT(i, j) flowInput(((i - 1) * n + j) * 2, ((i - 1) * n + (j + 1)) * 2 - 1, 1); // 오른쪽과 연결



int v, e;


struct Link

{


Link(int u, int capacity)

{


this->u = u;

this->flow = 0;

this->capacity = capacity;

this->cosine = NULL;

next = NULL;


}


int u;


int capacity;


int flow;


Link *cosine;




Link *next;




};





#include<iostream>

using namespace std;


struct Item

{

int u;

int localResidue;


Item *next;

};



Link **graph;

int *seen;

int *parent;

//int seen[1000];

//int parent[1000];

Item queue[1000];


Item *front;

Item *back;


//int front;

//int back;


int min(int a, int b)

{

return (a<b) ? a : b;

}



void push(Item val){

if (front == NULL)

{

front = new Item(val);

back = front;

}

else 

{

back->next = new Item(val);

back = back->next;


}

}

Item pop(){

if (back != NULL)

{

Item ret(*front);

Item* temp = front;

front = front->next;

if (front == NULL)

back = NULL;

delete temp;

return ret;

}

}

bool queueEmpty()

{

return back == NULL;

}




void doFlow(int head, int flow)

{


for (Link* temp = graph[parent[head]]; temp != NULL; temp = temp->next)

{

if (temp->u == head)

{


temp->flow += flow;

temp->cosine->flow -= flow;


}


}


}




int run = 1;




void bfs(int n)

{


Item item;

item.u = n;

item.localResidue = 987654321;

item.next = NULL;

push(item);


Item nextItem;

nextItem.u = 0;

nextItem.localResidue = 987654321;

nextItem.next = NULL;



while (!queueEmpty())

{

Item i = pop();

seen[i.u] = 2;

//cout << i << endl;


for (Link *temp = graph[i.u]; temp != NULL; temp = temp->next)

{

if (seen[temp->u] == 0 && temp->capacity > temp->flow)

{

parent[temp->u] = i.u;

nextItem.u = temp->u;

nextItem.localResidue = min(i.localResidue, temp->capacity - temp->flow);

push(nextItem);

seen[temp->u] = 1;



if (nextItem.u == v + 1)

{

int head = v + 1;

while (head != 0)

{

doFlow(head, nextItem.localResidue);

head = parent[head];

}

run = 1;

return;


}


}


}


}




if (nextItem.u != v + 1)

{

run = 0;

}


}




void bfs_all()

{


for (int i = 0; i < v; i++)

if (seen[i] == 0)

bfs(i);


}



fstream fin;


int flow();

void flowInput(int a, int b, int c);


int main()

{

int n, m, k, flowAmount = 0;

char tmp;


fin.open("input.txt");


fin >> n >> m >> k;


v = n + m;


graph = new Link*[v + 2];

seen = new int[v + 2];

parent = new int[v + 2];


memset(graph, 0, sizeof(Link*) * (v + 2));

for(int i = 0; i < k; i++)


for(int i = 0; i < k; i++)

{

int a, b;

fin >> a >> b;

flowInput(0, a, 1);

flowInput(a, n + b, 1);

flowInput(n + b, n + m + 1, 1);

}

int ret = flow();

std::cout << "Max Flow is " << ret << endl;

}


void flowInput(int a, int b, int c)

{


//int a, b, c;


std::cout << a << " " << b << " " << c << endl;


Link *aLink = NULL;

Link *bLink = NULL;



bool isExist = false;


if (graph[a] == NULL)

{

graph[a] = new Link(b, c);

aLink = graph[a];

}

else

{

Link *temp = NULL;

for (temp = graph[a]; temp != NULL; temp = temp->next)

{

if (temp->u == b)

{

isExist = true;

temp->capacity = c;

}


}


if (!isExist)

{


for (temp = graph[a]; temp->next != NULL; temp = temp->next);

temp->next = new Link(b, c);

aLink = temp->next;


}


}


isExist = false;


if (graph[b] == NULL)

{

graph[b] = new Link(a, 0);

bLink = graph[b];


}

else

{

Link *temp = NULL;


for (temp = graph[b]; temp != NULL; temp = temp->next)

{


if (temp->u == a)

{

isExist = true;

break;

}


}


if (!isExist)

{

for (temp = graph[b]; temp->next != NULL; temp = temp->next);

temp->next = new Link(a, 0);

bLink = temp->next;


}


}

if (aLink && bLink)

{


aLink->cosine = bLink;

bLink->cosine = aLink;


}

}


int flow()

{


while (run)

{

front = NULL;

back = NULL;

/*

front = 0;

back = 0;*/

memset(seen, 0, sizeof(int) * (v + 2));

memset(parent, -1, sizeof(int) * (v + 2));

//memset(parent, -1, sizeof(parent));

//memset(seen, 0, sizeof(seen));

bfs(0);


while (!queueEmpty())

pop();


}




//for (int i = 1; i <= v; i++)

//{

// std::cout << parent[i] << endl;

//}




int result = 0;

for (Link *temp = graph[0]; temp != NULL; temp = temp->next)

{

result += temp->flow;

}




return result;




}

'공부 > 알고리즘2' 카테고리의 다른 글

Topological Sort + Bellman Ford로 O(e) 시간에 풀기  (0) 2014.11.13
RACE bellman Ford  (0) 2014.11.06
maxflow 탈출문제  (0) 2014.11.06
bfs  (0) 2014.10.16
BFS, DFS, Topological Sort  (0) 2014.10.02
posted by pflower 2014. 11. 6. 09:56

#include <iostream>

#include <iostream>

#include <fstream>



using namespace std;



#define UP(i, j) flowInput(((i - 1) * n + j) * 2, ((i - 2) * n + j) * 2 - 1, 1); // 위와 연결

#define DOWN(i, j) flowInput(((i - 1) * n + j) * 2, (i * n + j) * 2 - 1, 1); // 아래와 연결

#define LEFT(i, j) flowInput(((i - 1) * n + j) * 2, ((i - 1) * n + (j - 1)) * 2 - 1, 1); // 왼쪽과 연결

#define RIGHT(i, j) flowInput(((i - 1) * n + j) * 2, ((i - 1) * n + (j + 1)) * 2 - 1, 1); // 오른쪽과 연결



int v, e;


struct Link

{


Link(int u, int capacity)

{


this->u = u;

this->flow = 0;

this->capacity = capacity;

this->cosine = NULL;

next = NULL;


}


int u;


int capacity;


int flow;


Link *cosine;




Link *next;




};





#include<iostream>

using namespace std;

template<typename T>

struct Node{

T data;

Node* next;

Node(T d, Node* n = NULL) : data(d), next(n){}

};


struct Item

{

int u;

int localResidue;


Item *next;

};



Link **graph;

int *seen;

int *parent;

//int seen[1000];

//int parent[1000];

Item queue[1000];


Item *front;

Item *back;


//int front;

//int back;


int min(int a, int b)

{

return (a<b) ? a : b;

}



void push(Item val){

if (front == NULL)

{

front = new Item(val);

back = front;

}

else 

{

back->next = new Item(val);

back = back->next;


}

}

Item pop(){

if (back != NULL)

{

Item ret(*front);

Item* temp = front;

front = front->next;

if (front == NULL)

back = NULL;

delete temp;

return ret;

}

}

bool queueEmpty()

{

return back == NULL;

}




void doFlow(int head, int flow)

{


for (Link* temp = graph[parent[head]]; temp != NULL; temp = temp->next)

{

if (temp->u == head)

{


temp->flow += flow;

temp->cosine->flow -= flow;


}


}


}




int run = 1;




void bfs(int n)

{


Item item;

item.u = n;

item.localResidue = 987654321;

item.next = NULL;

push(item);


Item nextItem;

nextItem.u = 0;

nextItem.localResidue = 987654321;

nextItem.next = NULL;



while (!queueEmpty())

{

Item i = pop();

seen[i.u] = 2;

//cout << i << endl;


for (Link *temp = graph[i.u]; temp != NULL; temp = temp->next)

{

if (seen[temp->u] == 0 && temp->capacity > temp->flow)

{

parent[temp->u] = i.u;

nextItem.u = temp->u;

nextItem.localResidue = min(i.localResidue, temp->capacity - temp->flow);

push(nextItem);

seen[temp->u] = 1;



if (nextItem.u == v + 1)

{

int head = v + 1;

while (head != 0)

{

doFlow(head, nextItem.localResidue);

head = parent[head];

}

run = 1;

return;


}


}


}


}




if (nextItem.u != v + 1)

{

run = 0;

}


}




void bfs_all()

{


for (int i = 0; i < v; i++)

if (seen[i] == 0)

bfs(i);


}



fstream fin;


int flow();

void flowInput(int a, int b, int c);


int main()

{

int n, flowAmount = 0;

char tmp;


fin.open("escape_input6.txt");


fin >> n;


v = n * n * 2;


graph = new Link*[v + 2];

seen = new int[v + 2];

parent = new int[v + 2];


memset(graph, 0, sizeof(Link*) * (v + 2));

for (int i = 1; i <= n; i++)

for (int j = 1; j <= n; j++)

{

fin >> tmp;

if (tmp == '1') // 나오는곳인 경우

{

++flowAmount;

flowInput(0, ((i - 1) * n + j) * 2 - 1, 1); // 시작점과 연결

}

flowInput(((i - 1) * n + j) * 2 - 1, ((i - 1) * n + j) * 2, 1); // 자신의 기본 연결


if (i == 1 || i == n || j == 1 || j == n) // 끝나는 곳인 경우

{

flowInput(((i - 1) * n + j) * 2, n * n * 2 + 1, 1); // 끝점과 연결

}

if (i == 1) // 위 가장자리

{

DOWN(i, j)

if (j == 1) // 왼 가장 자리

{

RIGHT(i, j)

}

else if (j == n) // 오른 가장 자리

{

LEFT(i, j)

}

else

{

RIGHT(i, j)

LEFT(i, j)

}

}

else if (i == n) // 아래 가장 자리

{

UP(i, j)

if (j == 1) // 왼 가장 자리

{

RIGHT(i, j)

}

else if (j == n) // 오른 가장 자리

{

LEFT(i, j)

}

else

{

RIGHT(i, j)

LEFT(i, j)

}

}

else if (j == 1) // 왼 가장 자리

{

DOWN(i, j)

UP(i, j)

RIGHT(i, j)

}

else if (j == n) // 오른 가장 자리

{

DOWN(i, j)

UP(i, j)

LEFT(i, j)

}

else // 가장자리 아님

{

DOWN(i, j)

UP(i, j)

LEFT(i, j)

RIGHT(i, j)

}

}

int ret = flow();

std::cout << "start : " << flowAmount << ((ret == flowAmount) ? " = " : " != ") << "max : "<< ret << endl;

std::cout << ((ret == flowAmount) ? "escape success!!" : "escape fail!!") << endl;

}


void flowInput(int a, int b, int c)

{


//int a, b, c;


std::cout << a << " " << b << " " << c << endl;


Link *aLink = NULL;

Link *bLink = NULL;



bool isExist = false;


if (graph[a] == NULL)

{

graph[a] = new Link(b, c);

aLink = graph[a];

}

else

{

Link *temp = NULL;

for (temp = graph[a]; temp != NULL; temp = temp->next)

{

if (temp->u == b)

{

isExist = true;

temp->capacity = c;

}


}


if (!isExist)

{


for (temp = graph[a]; temp->next != NULL; temp = temp->next);

temp->next = new Link(b, c);

aLink = temp->next;


}


}


isExist = false;


if (graph[b] == NULL)

{

graph[b] = new Link(a, 0);

bLink = graph[b];


}

else

{

Link *temp = NULL;


for (temp = graph[b]; temp != NULL; temp = temp->next)

{


if (temp->u == a)

{

isExist = true;

break;

}


}


if (!isExist)

{

for (temp = graph[b]; temp->next != NULL; temp = temp->next);

temp->next = new Link(a, 0);

bLink = temp->next;


}


}

if (aLink && bLink)

{


aLink->cosine = bLink;

bLink->cosine = aLink;


}

}


int flow()

{


while (run)

{

front = NULL;

back = NULL;

/*

front = 0;

back = 0;*/

memset(seen, 0, sizeof(int) * (v + 3));

memset(parent, -1, sizeof(int) * (v + 3));

//memset(parent, -1, sizeof(parent));

//memset(seen, 0, sizeof(seen));

bfs(0);


while (!queueEmpty())

pop();


}




//for (int i = 1; i <= v; i++)

//{

// std::cout << parent[i] << endl;

//}




int result = 0;

for (Link *temp = graph[0]; temp != NULL; temp = temp->next)

{

result += temp->flow;

}




return result;




}

'공부 > 알고리즘2' 카테고리의 다른 글

RACE bellman Ford  (0) 2014.11.06
maxflow bipar  (0) 2014.11.06
bfs  (0) 2014.10.16
BFS, DFS, Topological Sort  (0) 2014.10.02
다잌스트라  (0) 2014.10.02

bfs

posted by pflower 2014. 10. 16. 11:44

#include <iostream>

using namespace std;

#include <iostream>

#include <fstream>



using namespace std;




int v, e;




struct Link

{


Link(int u, int capacity)

{

this->u = u;

this->flow = 0;

this->capacity = capacity;

this->cosine = NULL;

next = NULL;

}

int u;

int capacity;

int flow;

Link *cosine;


Link *next;


};


struct Item

{

int u;

int localResidue;

};


struct ansLink

{

ansLink(int v, int u) {this->v = v; this->u = u; next = NULL;}

int u, v;

ansLink *next;

};


ansLink* store = NULL;


Link* graph[500];


int seen[500];


Item queue[500];


int parent[500];


int front;


int back;



int min(int a, int b)

{

return (a<b) ? a : b;

}



void push(Item val)

{


queue[back] = val;

back = (back + 1) % 500;


}




Item pop()

{

Item ret = queue[front];

front = (front + 1) % 500;

return ret;

}




bool queueEmpty()

{

return back == front;

}


void doFlow(int head, int flow)

{

for(Link* temp = graph[parent[head]]; temp != NULL; temp = temp->next)

{

if(temp->u == head)

{

temp->flow += flow;

temp->cosine->flow -= flow;

}

}

}


int run = 1;


void bfs(int n)

{

Item item;

item.u = n;

item.localResidue = 987654321;

push(item);

Item nextItem;

nextItem.u = 0;

nextItem.localResidue = 987654321;


while(!queueEmpty())

{

Item i = pop();

seen[i.u] = 2;


//cout << i << endl;

for(Link *temp = graph[i.u]; temp != NULL; temp = temp->next)

{

if(seen[temp->u] == 0 && temp->capacity > temp->flow)

{


parent[temp->u] = i.u;

if(store == NULL)

{

store = new ansLink(i.u);

}

else

{

ansLink *ansHead = NULL;

for(ansHead = store; ansHead->next != NULL; ansHead = ansHead->next)

{

store->next = new ansLink(i.u);

}

}



nextItem.u = temp->u;

nextItem.localResidue = min(i.localResidue, temp->capacity - temp->flow);

push(nextItem);

seen[temp->u] = 1;


if(nextItem.u == v + 1)

{

int head = v+1;

while(head != 0)

{

doFlow(head, nextItem.localResidue);

head = parent[head];

}

run = 1;

return;

}

}

}

}


if(nextItem.u != v + 1)

{

run = 0;

}

}




void bfs_all()

{

for(int i = 1; i <= v; i ++)

if(seen[i] == 0)

bfs(i);


}




int main()

{

fstream fin("input.txt");




fin >> v >> e;


for(int i = 0; i < e; i ++)

{


int a, b, c;


fin >> a >> b >> c;


Link *aLink = NULL;

Link *bLink = NULL;


bool isExist = false;


if(graph[a] == NULL)

{

graph[a] = new Link(b, c);

aLink = graph[a];

}

else

{

Link *temp = NULL;

for(temp = graph[a]; temp->next != NULL; temp = temp->next)

{

if(temp->next->u == b)

{

isExist = true;

temp->next->capacity = c;

}

}

if(!isExist)

{

temp->next = new Link(b, c);

aLink = temp->next;

}

}


isExist = false;


if(graph[b] == NULL)

{

graph[b] = new Link(a, 0);

bLink = graph[b];

}

else

{

Link *temp = NULL;

for(temp = graph[b]; temp->next != NULL; temp = temp->next)

{

if(temp->next->u == a)

{

isExist = true;

break;

}

}

if(!isExist)

{

temp->next = new Link(a, 0);

bLink = temp->next;

}

}


if(aLink && bLink)

{

aLink->cosine = bLink;

bLink->cosine = aLink;

}


}



while(run)

{

back = 0;

front = 0;

memset(parent, -1, sizeof(parent));

memset(seen, 0, sizeof(seen));

bfs(0);

}


for(int i = 1; i <= v; i ++)

{


cout << parent[i] << endl;


}


int result = 0;

for(Link *temp = graph[0]; temp != NULL; temp = temp->next)

{

result += temp->flow;

}


cout << result << endl;


}

'공부 > 알고리즘2' 카테고리의 다른 글

maxflow bipar  (0) 2014.11.06
maxflow 탈출문제  (0) 2014.11.06
BFS, DFS, Topological Sort  (0) 2014.10.02
다잌스트라  (0) 2014.10.02
[알고리즘2] Heap, adjList and Prim  (0) 2014.09.25