Skip to content

Commit

Permalink
Added brandlarmet
Browse files Browse the repository at this point in the history
  • Loading branch information
s4wny committed Nov 22, 2014
1 parent 8853e08 commit 654eeae
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Strukturen är såhär `/uppgiftens id/ditt namn(-typ av lösning / version).[sp
- [Plocka äpplen](https://po.kattis.com/problems/apples)
- [Åtta spelet](https://po.kattis.com/problems/attaspelet) Grafproblem / SSSP problem. Svårare än Erdös nummer. Implicit graph istället för explicit.
- [Lastfärjan](https://po.scrool.se/problems/lastafarjan) Går att fuska till sig O(N) lösning. Riktiga lösningen är dock O(N^3) med DP.
- [Brandlarmet](https://po.kattis.com/problems/brandlarmet) Grafproblem / SSSP.

### Olösta

Expand All @@ -63,6 +64,7 @@ Urval av algoritmer vi stött på (inom parantes i hur många uppgifter):

- [Ternary search](http://en.wikipedia.org/wiki/Ternary_search) (1)
- [Golden section search](http://en.wikipedia.org/wiki/Golden_section_search) (1)
- [Dijkstras algoritm](https://www.youtube.com/watch?v=8Ls1RqHCOPw) (3)

Awesomenes slides av Johan Sannemo och Aron Granberg:

Expand Down
21 changes: 21 additions & 0 deletions brandlarmet/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
9
1 4
u
i
i
i
i
u
u
i
i
9
2 1 13 o
3 1 11 o
4 1 5 o
5 3 2 l
6 2 12 o
7 2 4 o
8 3 2 l
8 7 3 o
8 0 17 o
100 changes: 100 additions & 0 deletions brandlarmet/sawny.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>

using namespace std;

typedef pair<int, bool> pib;
typedef pair<int, pib> pipib;

int INF = 99999999;
int N, S, Y, M, P, Q, T;
vector<vector<pipib>> al;

/**
* Dijkstras algorithm för SSSP, O(E+V log V)
* Bra tutorial finns på YT: https://www.youtube.com/watch?v=8Ls1RqHCOPw
*
* Globala variabler: N, AL
*/
vector<int> SSSP(int start, bool hasKey) {
vector<int> dist(N, INF);
dist[start] = 0;
priority_queue< pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > pq;
pq.push(make_pair(0, start));

while(!pq.empty()) {
pair<int, int> front = pq.top(); pq.pop();
int d = front.first;
int u = front.second;

if(d > dist[u]) continue;
for(int j = 0; j < al[u].size(); j++) {
pipib v = al[u][j];

//Låst dörr, går ej att gå igenom (Notera att denna rad är enda ändringen i algoritmen, resten är standard implementation av dijkstras)
if(v.second.second && !hasKey) continue;

if(dist[u] + v.second.first < dist[v.first]) {
dist[v.first] = dist[u] + v.second.first;
pq.push(make_pair(dist[v.first], v.first));
}
}
}

return dist;
}

int main() {
ios_base::sync_with_stdio(false);
wbstdin;

char outOrIn, lockedOrNot;
vector<bool> outside;
pair<int, bool> weightAndState;

cin >> N >> S >> Y; //S = start, Y = key
for(int i = N; i > 0; i--) {
cin >> outOrIn;
outside.push_back( outOrIn == 'u' );
}


// Bygg en AdjacencyList
al.assign(N, vector<pipib>());
cin >> M;
while(M--) {
cin >> P >> Q >> T >> lockedOrNot;
weightAndState = make_pair(T, (lockedOrNot == 'l') );

al[P].push_back(make_pair(Q, weightAndState));
al[Q].push_back(make_pair(P, weightAndState));
}


// Kör SSSP på AL med låsta dörrar
vector<int> dist;
dist = SSSP(S, false);


// Kortaste vägen
int shortest = INF;
for(int i = 0; i < outside.size(); i++) {
if(outside[i])
shortest = min(shortest, dist[i]);
}


int keyDist = dist[Y];

// Kör Dijkstras med alla dörrar UPPLÅSTA
dist = SSSP(Y, true);
for(int i = 0; i < outside.size(); i++)
if(outside[i])
shortest = min(shortest, dist[i] + keyDist); //Notera att shortest vid loop 1 = kortaste vägen utan nyckel


cout << shortest;
}

0 comments on commit 654eeae

Please sign in to comment.