You are on page 1of 12

ALGORITMOPARALEERUNGRAFONOPONDERADODIRIGIDO

#include<iostream>
#include<cstdio>
#include<vector>
usingnamespacestd;
typedefvector<int>vi;
#defineREP(i,a,b)\
for(inti=int(a);i<=int(b);i++)
#defineTRvi(c,it)\
for(vi::iteratorit=(c).begin();it!=(c).end();it++)
#defineREDIRECT_INPUTfreopen("input.txt","r",stdin);
vector<vi>AdjList;
intmain(){
REDIRECT_INPUT
intv,e,s,t;
scanf("%d%d",&v,&e);//leernumerodenodosyarcos
AdjList.assign(v,vi());
while(e){
scanf("%d%d",&s,&t);//leerorigenydestinodelarco
AdjList[s].push_back(t);
}
REP(i,0,v1){
printf("%d:",i);
TRvi(AdjList[i],it){
printf("%d",*it);
}
printf("\n");
}
return0;
}

ALGORITMOPARALEERUNGRAFOPONDERADODIRIGIDO
#include<iostream>
#include<cstdio>
#include<vector>
usingnamespacestd;
typedefpair<int,int>ii;
typedefvector<int>vi;
typedefvector<ii>vii;
#defineREP(i,a,b)\
for(inti=int(a);i<=int(b);i++)
#defineTRvii(c,it)\
for(vii::iteratorit=(c).begin();it!=(c).end();it++)
#defineREDIRECT_INPUTfreopen("input.txt","r",stdin);
vector<vii>AdjList;
intmain(){
REDIRECT_INPUT
intv,e;//numberofvertexandedges
ints,t,w;//start,targetandweight
scanf("%d%d",&v,&e);
AdjList.assign(v,vii());
while(e){
scanf("%d%d%d",&s,&t,&w);
AdjList[s].push_back(ii(t,w));
}
printf("MostrarGrafo\n");
REP(i,0,v1){
printf("%d:",i);
TRvii(AdjList[i],it){
printf("(%d,%d)",it>first,it>second);
}
printf("\n");
}
}

RECORRIDODFSENUNGRAFONOPONDERADO
#include<iostream>
#include<cstdio>
#include<vector>
usingnamespacestd;
typedefvector<int>vi;
#defineREP(i,a,b)\
for(inti=int(a);i<=int(b);i++)
#defineTRvi(c,it)\
for(vi::iteratorit=(c).begin();it!=(c).end();it++)
#defineREDIRECT_INPUTfreopen("input.txt","r",stdin);
#defineDFS_WHITE1
#defineDFS_BLACK1
vector<vi>AdjList;
vidfs_num;
voidinitDFS(intv){
dfs_num.assign(v,DFS_WHITE);
//memset(dfs_num,DFS_WHITE,sizeofdfs_num);
}
voiddfs(intu){
printf("%d",u);
dfs_num[u]=DFS_BLACK;//visitado
TRvi(AdjList[u],v){
if(dfs_num[*v]==DFS_WHITE){//novisitado?
dfs(*v);
}
}
}
intmain(){
REDIRECT_INPUT
intv,e,s,t;
scanf("%d%d",&v,&e);//leernumerodenodosyarcos
AdjList.assign(v,vi());
while(e){
scanf("%d%d",&s,&t);//leerorigenydestinodelarco
AdjList[s].push_back(t);
}
REP(i,0,v1){

printf("%d:",i);
TRvi(AdjList[i],it){
printf("%d",*it);
}
printf("\n");
}
printf("RecorridoDFS:\n");
initDFS(v);
dfs(0);
return0;
}


RECORRIDO
BFS
ENUNGRAFONOPONDERADO

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
#include<map>
usingnamespacestd;
typedefvector<int>vi;
#defineREP(i,a,b)\
for(inti=int(a);i<=int(b);i++)//atob,andvariableiis
local!
#defineTRvi(c,it)\
for(vi::iteratorit=(c).begin();it!=(c).end();it++)
#defineREDIRECT_INPUTfreopen("input.txt","r",stdin);
#defineREDIRECT_OUTPUTfreopen("output.txt","w",stdout);
vector<vi>AdjList;
queue<int>q;
map<int,int>dist;
voidinitBFS(){
dist.clear();
}
voidbfs(ints){
q.push(s);
dist[s]=0;//startfromsource
while(!q.empty()){
intu=q.front();
q.pop();//queue:layerbylayer!
printf("Visited%d,Layer%d\n",u,dist[u]);
TRvi(AdjList[u],it){//foreachneighboursofu
if(!dist.count(*it)){//dist.find(it)!=dist.end()
alsowork
dist[*it]=dist[u]+1;//ifitnotvisited
q.push(*it);//enqueueitfornextsteps
}
}
}
}
intmain(){
REDIRECT_INPUT
REDIRECT_OUTPUT

intv,e,s,t;
scanf("%d%d",&v,&e);//leernumerodenodosyarcos
AdjList.assign(v,vi());
while(e){
scanf("%d%d",&s,&t);//leerorigenydestinodelarco
AdjList[s].push_back(t);
//AdjList[t].push_back(s);//undirected
}
REP(i,0,v1){
printf("%d:",i);
TRvi(AdjList[i],it){
printf("%d",*it);
}
printf("\n");
}
printf("RecorridoBFS:\n");
initBFS();
bfs(0);
return0;
}


ORDENACIONTOPOLOGICAEN
UNGRAFONOPONDERADO

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
usingnamespacestd;
typedefvector<int>vi;
#defineREP(i,a,b)\
for(inti=int(a);i<=int(b);i++)
#defineTRvi(c,it)\
for(vi::iteratorit=(c).begin();it!=(c).end();it++)
#defineREDIRECT_INPUTfreopen("input.txt","r",stdin);
#defineDFS_WHITE1//unvisited
#defineDFS_BLACK1//visitedandcompleted
vector<vi>AdjList;
vidfs_num;
vitopologicalSort;
voidtopoVisit(intu){
dfs_num[u]=DFS_BLACK;
TRvi(AdjList[u],it){
if(dfs_num[*it]==DFS_WHITE){
topoVisit(*it);
}
}
topologicalSort.push_back(u);//thisistheonlychange
}
intmain(){
REDIRECT_INPUT
intv,e,s,t;
scanf("%d%d",&v,&e);//leernumerodenodosyarcos
AdjList.assign(v,vi());
while(e){
scanf("%d%d",&s,&t);//leerorigenydestinodelarco
AdjList[s].push_back(t);
}
REP(i,0,v1){
printf("%d:",i);
TRvi(AdjList[i],it){

printf("%d",*it);
}
printf("\n");
}
printf("TopologicalSort:\n");
topologicalSort.clear();
dfs_num.assign(v,DFS_WHITE);
REP(i,0,v1){
if(dfs_num[i]==DFS_WHITE){
topoVisit(i);
}
}
reverse(topologicalSort.begin(),topologicalSort.end());
REP(i,0,topologicalSort.size()1){
printf("%d\n",topologicalSort[i]);
}
return0;
}


COMPONENTESCONECTADOS
EN
UNGRAFONOPONDERADO

#include<iostream>
#include<cstdio>
#include<vector>
usingnamespacestd;
typedefvector<int>vi;
#defineREP(i,a,b)\
for(inti=int(a);i<=int(b);i++)
#defineTRvi(c,it)\
for(vi::iteratorit=(c).begin();it!=(c).end();it++)
#defineREDIRECT_INPUTfreopen("input.txt","r",stdin);
#defineDFS_WHITE1
#defineDFS_BLACK1
vector<vi>AdjList;
vidfs_num;
intnumComponent;
voidinitDFS(intv){
dfs_num.assign(v,DFS_WHITE);
numComponent=0;
}
voiddfs(intu){
printf("%d",u);
dfs_num[u]=DFS_BLACK;//visitado
TRvi(AdjList[u],v){
if(dfs_num[*v]==DFS_WHITE){//novisitado?
dfs(*v);
}
}
}
intmain(){
REDIRECT_INPUT
intv,e,s,t;
scanf("%d%d",&v,&e);//leernumerodenodosyarcos
AdjList.assign(v,vi());
while(e){
scanf("%d%d",&s,&t);//leerorigenydestinodelarco
AdjList[s].push_back(t);
AdjList[t].push_back(s);//undirected
}

REP(i,0,v1){
printf("%d:",i);
TRvi(AdjList[i],it){
printf("%d",*it);
}
printf("\n");
}
printf("Componentes\n");
initDFS(v);
//findingconnectedcomponentsinunidirectedgraph
REP(i,0,v1){
if(dfs_num[i]==DFS_WHITE){
printf("Component%d,visit:",++numComponent);
dfs(i);
printf("\n");
}
}
return0;
}


DIJKSTRA

UNGRAFOPONDERADO
EN

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
usingnamespacestd;
typedefpair<int,int>ii;
typedefvector<int>vi;
typedefvector<ii>vii;
#defineINF2147483647//2^311
#defineREP(i,a,b)\
for(inti=int(a);i<=int(b);i++)
#defineTRvii(c,it)\
for(vii::iteratorit=(c).begin();it!=(c).end();it++)
#defineREDIRECT_INPUTfreopen("input.txt","r",stdin);
vector<vii>AdjList;
intmain(){
REDIRECT_INPUT
intv,e;//numberofvertexandedges
ints,t,w;//start,targetandweight
scanf("%d%d",&v,&e);
AdjList.assign(v,vii());
while(e){
scanf("%d%d%d",&s,&t,&w);
AdjList[s].push_back(ii(t,w));
}
printf("MostrarGrafo\n");
REP(i,0,v1){
printf("%d:",i);
TRvii(AdjList[i],it){
printf("(%d,%d)",it>first,it>second);
}
printf("\n");
}
intstart=0;
//Dijstraroutine
vector<int>dist(v,INF);//INF=2.10^9notMAX_INT
dist[start]=0;
priority_queue<ii,vector<ii>,greater<ii>>pq;
pq.push(ii(0,start));//sortbasedonincreasingdistance

while(!pq.empty()){//mainloop
iitop=pq.top();pq.pop();
intd=top.first,u=top.second;
if(d==dist[u]){
TRvii(AdjList[u],it){
intv=it>first,weight_u_v=it>second;
if(dist[u]+weight_u_v<dist[v]){
dist[v]=dist[u]+weight_u_v;
pq.push(ii(dist[v],v));
}
}
}
}
REP(i,0,v1){
printf("SSSP(%d,%d)=%d\n",start,i,dist[i]);
}
}

You might also like