NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <cmath>
#include <cstdio>
#include <vector>
#include <set>
#include <queue>

using namespace std;

struct Edge{
int v1;
int v2;
Edge(int v1, int v2){
this->v1 = v1;
this->v2 = v2;
}
};

struct Vertex{
vector<Edge> adj;
bool visited = false;
int distance = INT32_MAX;
};

struct vertexComparator {
bool operator()(Vertex i, Vertex j) {
return i.distance > j.distance;
}
};

int main() {
int numberOfBuildings;
int numberOfFloor;
scanf("%d%d",&numberOfBuildings,&numberOfFloor);
vector<Vertex> graph;
vector<int> distTo(numberOfBuildings*numberOfFloor+2);

for(int j = 0; j < numberOfBuildings*numberOfFloor+2; j++){
Vertex v1;
graph.push_back(v1);
}

for (int i = 0; i < numberOfFloor*numberOfBuildings; i += numberOfBuildings){
graph[0].adj.push_back(Edge(0,i+1));
}

for (int k = numberOfBuildings; k <= numberOfBuildings*numberOfFloor; k += numberOfBuildings) {
graph[k].adj.push_back(Edge(k,numberOfBuildings*numberOfFloor+1));
}

for(int j = 1; j <= numberOfFloor; j++){
for (int i = 1; i <= numberOfBuildings; ++i){
int n1;
scanf("%d",&n1);
graph[(j-1)*numberOfBuildings + i].distance = n1;

for (int k=0; k<numberOfFloor-j+1; k++){
if(i != numberOfBuildings)
graph[(j-1)*numberOfBuildings + i].adj.push_back(Edge((j-1)*numberOfBuildings + i, (j-1)*numberOfBuildings + i + k*numberOfBuildings+1));
}
}
}

bool* visited = new bool[numberOfBuildings*numberOfFloor];
for(int j = 0; j < numberOfBuildings*numberOfFloor; j++){
visited[j] = false;
}

priority_queue<Vertex, vector<Vertex>, vertexComparator> q;

int current = 0, final = numberOfBuildings*numberOfFloor + 1;

q.push(graph[current]);

while (true){
if(current == final)
break;
for (int i = 0; i < graph[current].adj.size(); ++i) {
if(!visited[graph[current].adj[i].v2]){
graph[graph[current].adj[i].v2].distance += graph[current].distance;
q.push(graph[graph[current].adj[i].v2]);
visited[graph[current].adj[i].v2] = true;
} else {
if(graph[graph[current].adj[i].v2].distance < )
}
}


int startPoint = 0;
// Set is a data structure that is implemented via red-black trees(something like AVL).
// It always has the minimum element at the 0th index. The good thing is it is iteratable
// while priority queue is not which enables coder change the things inside the set. You can make use of this feature though in this code I did not.
// Note that this is a pair set(pairs are sorted according to their first fields). But you can
// construct a vertex set using the comparator above. I don't know why I did not.
set<pair<int,int> > distances;
// Starting point has 0 distance to itself.
distances.insert(make_pair(0,startPoint));

int closest = startPoint;
// While the set is not empty go on
while(distances.size() > 0) {
// If the vertex is not popped out of the set before process it. Otherwise
// you dont have to do anything. Note that the latter will occur if a shorter path
// is discovered to anu of the vertices.
if (!visited[closest]) {
visited[closest] = true; // visit the node
vector<Edge> discovereds = graph[closest].adj; // Get the new edges from thr graph.
int distToV1 = distances.begin()->first;
graph[closest].distance = distToV1; // Since the vertex is popped out of the set the shortest path to it is found.
// Push the new edges into the set.
for (int j = 0; j < discovereds.size(); j++) {
int v2 = discovereds[j].v2;
int w = discovereds[j].w;
distances.insert(make_pair(distToV1 + w, v2));
}
}
// Now erase the head of the set
distances.erase(distances.begin());
// Now the closest is the new head.
closest = distances.begin()->second;

}
return 0;
}

     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.