Skip to content

Instantly share code, notes, and snippets.

@pr00thmatic
Created May 19, 2014 03:03
Show Gist options
  • Save pr00thmatic/9e27bfeb83bd295425ca to your computer and use it in GitHub Desktop.
Save pr00thmatic/9e27bfeb83bd295425ca to your computer and use it in GitHub Desktop.
//#define NDEBUG
#define INF numeric_limits<int>::max()
#include <limits>
#include <string>
#include <queue>
#include <math.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <assert.h>
#include <set>
#include <map>
#include <cstdio>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<char> vc;
typedef pair<int,int> ii;
typedef pair<string,string> ss;
typedef vector<ii> vii;
typedef double db;
typedef vector<db> vdb;
typedef pair<db, db> dbdb;
int dijkstra(vector<vi> M) {
priority_queue<pair<int, ii> > exploring; //weight, i, j.
vector<vector<ii> > parents(M.size(), vector<ii>(M[0].size(), ii(-1,-1)));
vector<vi> d(M.size(), vi(M[0].size(), INF));
parents[0][0]=ii(-1,-1);
d[0][0]=0;
exploring.push(pair<int,ii>(0, ii(0,0)));
while(!exploring.empty()) {
ii explore = exploring.top().second; //i, j
int weight = exploring.top().first;
exploring.pop();
if(weight > d[explore.first][explore.second])
continue;
int fi=explore.first, fj=explore.second, si, sj;
cout << "on: " << fi << "," << fj << endl;
for(int di=-1; di<2; di++) {
for(int dj=-1; dj<2; dj++) {
si = fi+di;
sj = fj+dj;
cout << "ding!" << endl;
if(si>0 && si<M.size() &&
sj>0 && sj<M[0].size()) {
cout << "\tis inside the boundaries" << endl;
cout << di << "," << dj << endl;
if(abs(di) != abs(dj)) {
cout << "\tand is not a diagonal..." << endl;
if(d[fi][fj] + M[si][sj] < d[si][sj])
cout << "\tit is better than the another!" << endl;
} else {
cout << "'twas a diagonal :(" << endl;
}
} else {
cout << "not inside the boundaries" << endl; //won't print this: Segmentation fault
}
// if( si>0 && si<M.size() &&
// sj>0 && sj<M[0].size() &&
// (abs(di) != abs(dj)) &&
// (d[fi][fj] + M[si][sj] < d[si][sj]) ) {
// cout << "ding!" << endl;
// d[si][sj] = d[fi][fj];
// parents[si][sj] = ii(fi,fj);
// exploring.push(pair<int, ii>(d[si][sj], ii(si, sj)));
// }
}
}
}
return d[M.size()][M[0].size()];
}
int main(){
int tc;
scanf("%d", &tc);
while(tc--) {
int n, m;
scanf("%d %d", &n, &m);
vector<vi> M(n, vi(m));
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
scanf("%d", &M[i][j]);
cout << M[i][j] << " ";
}
cout << endl;
}
printf("%d\n", dijkstra(M));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment