Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions 13307130226/assignment13/1094.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include<iostream>
#include<string>
using namespace std;

int gre[300][300];
int val[300];
int n,m;
string tmp;
string ans;
bool nright;
bool nn;

bool check(int times){
for(int i=0;i<n;++i)val[i]=0;
for(int i='A';i<'A'+n;++i){
gre[i][0]=0;
for(int j='A';j<'A'+n;++j)
if(gre[i][j]==1)
++gre[i][0];
val[gre[i][0]]=i;
}
for(int i=0;i<n;++i)
if(val[i]==0)return false;
ans = "Sorted sequence determined after ";
ans +=(char)(times+'1');
ans +=" relations: ";
for(int i=n-1;i>=0;--i)
ans +=(char)val[i];
ans += '.';
return true;
}

int main(){
while(1){
cin >> n >> m;
if(n == 0 && m == 0)
break;
for(int i='A';i<'A'+n;++i)
for(int j='A';j<'A'+n;++j)
gre[i][j]=0;
nright=nn=false;
ans = "Sorted sequence cannot be determined.";
for(int ii=0;ii<m;++ii){
cin >> tmp;
if(nn)continue;
int a,b; //a<b
a = tmp[0];
b = tmp[2];
if(gre[b][a]==1){ans = "Inconsistency found after ";ans+=(char)(ii+'1');ans+=" relations.";nright=true;nn=true;continue;}
gre[a][b]=1;
for(int i='A';i<'A'+n;++i){
if(gre[b][i]==1)gre[a][i]=1;
if(gre[i][a]==1)gre[i][b]=1;
}
if(!nright)
nright=check(ii);
}
cout << ans << endl;
}
return 0;
}
71 changes: 71 additions & 0 deletions 13307130226/assignment13/bk.1094.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int n,m;
int big[300][300];
int sma[300][300];
char order[300];
int i,j,g,h;
string tmp;
bool nright,occo;
int num_n;
bool check(int nn){
bool nright=false;
for(int i='A';i<'A'+n;++i){
big[i][0]=0;
for(int j='A';j<'A'+n;++j)
if(big[i][j]==1)++big[i][0];
//cout << (char)i << big[i][0] << endl;
}
for(int i=n-1;i>=0;--i){
order[i]=-1;
for(int j='A';j<'A'+n;++j)
if(big[j][0]==i)
order[i]=j;
if(order[i]==-1){
nright=true;}
}
if(nright)return false;
else{
cout << "Sorted sequence determined after "<<nn<<" relations: ";
for(int i=n-1;i>=0;--i)
cout << order[i];
cout <<'.'<< endl;
}
return true;

}
int main(){
while(1){
cin >> n >> m;
cin.sync();
if(n == 0 && m == 0)
break;
cout << "mn" << n << m << endl << flush;
nright = false;
for(int i='A';i<='Z';++i)
for(int j='A';j<='Z';++j)
big[i][j]=sma[i][j]=0;
for(int i=0;i<m;++i){
//cout << 't' << i << endl;
getline(cin,tmp);

if(nright)continue;
//cin >> tmp;//g<h
g = tmp[0];
h = tmp[2];
big[g][h]=1;
if(big[h][g]==1){nright=true;cout << "Inconsistency found after "<<i+1<<" relations." << endl;continue;}
for(int j='A';j<='Z';++j){
if(big[h][j]==1)big[g][j]=1;
if(big[j][g]==1)big[j][h]=1;
}
nright=check(i+1);
}
if(!nright)
cout <<"Sorted sequence cannot be determined." << endl;
}
return 0;
}
90 changes: 90 additions & 0 deletions 13307130226/assignment13/p1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;

int P,T;
class way{
public:
int a,w;
bool operator<(const way& a)const{
return a.w>w;
}
}tmp;
vector<vector<way> >ways;

vector<int>dis;
vector<set<int> >from;
class comp{
public:
bool operator()(int a,int b){
return dis[a]>dis[b];
}
};
priority_queue<int,vector<int>,comp>points;
const int shif = 14;
const int mask = 0x3fff;
set<int>occo;
const int MAXD = 80000000;
void dfs(int now){
//cout << "dfs " << now <<' '<<from[now].size()<< endl;

for(set<int>::iterator i=from[now].begin();i!=from[now].end();++i){
//cout << ((*i)&mask) << endl;
occo.insert(*i);
if(((*i)&mask)!=0)
dfs((*i)&mask);
}
}
int main(){
cin >> P >> T;
ways.resize(P);
dis.resize(P);
from.resize(P);
for(int i=0;i<P;++i){
ways[i].clear();
dis[i]=MAXD;
//from[i].clear();
}
for(int i=0;i<T;++i){
int a,b,w;
cin >> a >> b >> w;
tmp.a=b;tmp.w=w;
ways[a].push_back(tmp);
tmp.a=a;
ways[b].push_back(tmp);
}
while(!points.empty())points.pop();
dis[0]=0;
points.push(0);
while(!points.empty()){
int now;
now = points.top();
points.pop();
//cout << now << endl;
if(dis[now]>dis[P-1])break;
for(int i=0;i<ways[now].size();++i){
int b,w;
b = ways[now][i].a;
w = ways[now][i].w;
if(dis[b]==dis[now]+w){
from[b].insert((i<<shif)+now);
}else if(dis[b]>dis[now]+w){
dis[b]=dis[now]+w;
from[b].clear();
from[b].insert((i<<shif)+now);
points.push(b);
}
}
}
occo.clear();
dfs(P-1);
int ans;
ans = 0;
for(set<int>::iterator i=occo.begin();i!=occo.end();++i)
ans += ways[(*i)&mask][(*i)>>shif].w;
cout << (ans<<1) << endl;
return 0;
}
59 changes: 59 additions & 0 deletions 13307130226/assignment13/p2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include<iostream>
#include<vector>
#include<cstdio>
using namespace std;

int N,K;
int set[55000];
int rank[55000];
int ans;
void getset(int a){

//cout << "b " << a << ' ' << set[a] << ' ' <<rank[a] << endl;
if(set[a]==a)return;
getset(set[a]);
rank[a]=(rank[a]+rank[set[a]])%3;
set[a]=set[set[a]];
//cout << "r " << a << ' ' << set[a] << ' ' <<rank[a] << endl;
}
int main(){
cin >> N >> K;
ans = 0;
for(int i=0;i<=N;++i){
set[i]=i;
rank[i]=0;
}
for(int i=0;i<K;++i){
int d,x,y;
cin >> d >> x >> y;
if(x>N||y>N){
++ans;
//cout << "lie" << endl;
continue;}
getset(x);getset(y);
if(d==1){
if(set[x]==set[y]&&(-rank[x]+rank[y]+3)%3!=0)
{++ans;
//cout << "lie" << endl;
}
if(set[x]!=set[y]){
rank[set[y]]=3+rank[x]-rank[y];
set[set[y]]=set[x];
}
}else if(d==2){
if(set[x]==set[y]&&(rank[x]-rank[y]+3)%3!=1)
{++ans;
//cout << "lie" << endl;
}
if(set[x]!=set[y]){
rank[set[x]]=3-rank[x]+rank[y]+1;
set[set[x]]=set[y];
}
}/*
for(int i=0;i<9;++i){
cout << 'r' << i << set[i] << rank[i]%3 << endl;
} */
}

cout << ans << endl;
}
64 changes: 64 additions & 0 deletions 13307130226/assignment13/p3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;

int N,s,p;
int pointsX[600],pointsY[600];
int sets[600];
int nset;
double ans;
class way{
public:
double lo;
int a,b;
way(){
lo=0.0;
a=b=0;
}
bool operator<(const way& a)const{
return a.lo<lo;
}
};
int getset(int a){
return sets[a]==a?a:sets[a]=getset(sets[a]);
}

priority_queue<way>ways;
way tmp;
int main(){
cin >> N;
while(N--){
cin >> s >> p;
for(int i=0;i<p;++i){
cin >> pointsX[i] >> pointsY[i];
sets[i]=i;
}
while(!ways.empty())ways.pop();
for(int i=0;i<p;++i)
for(int j=i+1;j<p;++j){
tmp.lo = (pointsX[i]-pointsX[j])*(pointsX[i]-pointsX[j])+(pointsY[i]-pointsY[j])*(pointsY[i]-pointsY[j]);
tmp.a = i;
tmp.b = j;
ways.push(tmp);
}
nset=p;
ans = 0;
while(!ways.empty()){
tmp = ways.top();
//cout << "tmp " << tmp.a << ' ' << tmp.b << ' ' <<tmp.lo <<endl;
ways.pop();
if(getset(tmp.a)==getset(tmp.b))continue;
--nset;
ans=ans>tmp.lo?ans:tmp.lo;
sets[sets[tmp.a]]=sets[tmp.b];
if(nset==s)break;
}
printf("%0.2f",sqrt(ans));
cout << endl;
}
return 0;
}