Skip to content

Commit

Permalink
UVaOJ
Browse files Browse the repository at this point in the history
  • Loading branch information
viraj071 committed Apr 25, 2015
1 parent 923df27 commit 2ab59ab
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 2 deletions.
75 changes: 75 additions & 0 deletions 10986 - Sending email/SendingEmail.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* SendingEmail.cpp
*
* Created on: Apr 25, 2015
* Author: viraj
*/

#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <sstream>
#include <fstream>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>

#define INF 1000000000

using namespace std;
vector<long long> val;
vector<vector<pair<int,int> > > neighbors;
int t,n,m,s,x,y,v;

void dijkstras(int s,int t){
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > pq;
pq.push(pair<int,int>(s,0));
val[s]=0;
while(!pq.empty()){
pair<int,int> top = pq.top();
pq.pop();
for(int j=0;j<neighbors[top.first].size();++j){
pair<int,int> x = neighbors[top.first][j];
if(val[x.first]>x.second+val[top.first]){
val[x.first]=x.second+val[top.first];
pq.push(make_pair(x.first,val[x.first]));
}
}
}
}


int main(){
int cases;
scanf("%d",&cases);
for(int i=1;i<=cases;++i){
scanf("%d %d %d %d",&n,&m,&s,&t);
val = vector<long long>(n,INF);
neighbors = vector<vector<pair<int,int> > > (n,vector<pair<int,int> >());
while(m--){
scanf("%d %d %d",&x,&y,&v);
neighbors[x].push_back(make_pair(y,v));
neighbors[y].push_back(make_pair(x,v));
}
dijkstras(s,t);
if(val[t]==INF){
cout<<"Case #"<<i<<": unreachable\n";
}
else{
cout<<"Case #"<<i<<": "<<val[t]<<"\n";
}
}
return 0;
}
105 changes: 105 additions & 0 deletions 315 - Network/Network.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Network.cpp
*
* Created on: Apr 25, 2015
* Author: viraj
*/



#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <sstream>
#include <fstream>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>

#define INF 1000000000

using namespace std;

vector<vector<int> > neighbors;
vector<bool> visited;
int nodes;

int getvisitedtotal(){
int total=0;
for(int i=0;i<visited.size();++i){
if(visited[i]){
total++;
}
}
return total;
}

void dfs(int source,int ignore){
visited = vector<bool> (nodes,false);
stack<int> st;
st.push(source);
while(!st.empty()){
int node = st.top();
st.pop();
visited[node]=true;
for(int j=0;j<neighbors[node].size();++j){
if(neighbors[node][j]!=ignore&&!visited[neighbors[node][j]]){
st.push(neighbors[node][j]);
}
}
}
}


int main(){
while(true){
scanf("%d",&nodes);
if(nodes==0){
break;
}
neighbors = vector<vector<int> > (nodes,vector<int> ());
string temp;
while(getline(cin,temp)){
if(temp=="0"){
break;
}
//cout<<temp<<endl;
istringstream ss(temp);
int source;
ss >> source;
int neighbor;
while(ss >> neighbor){
neighbors[source-1].push_back(neighbor-1);
neighbors[neighbor-1].push_back(source-1);
}
}
int ans=0;
for(int i=0;i<nodes;++i){
if(i==0){
dfs(1,0);
}
else{
dfs(0,i);
}
int nnodes = getvisitedtotal();
if(nnodes<(nodes-1)){

ans++;
}
}
printf("%d\n",ans);
}
return 0;
}
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
# UVaOJ
UVa Online Judge
UVaOJ
====

Solved Problems on UVa Online Judge(UVA)

I have shared the code for a few problems I have solved on UVa Online Judge.

If you feel any solution is incorrect, please feel free to email me at [email protected].

I would be glad to review and make the changes.

Thank you.

0 comments on commit 2ab59ab

Please sign in to comment.