-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
192 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |