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
94 changes: 94 additions & 0 deletions 13307130389/assignment13/t1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>

using namespace std ;

typedef pair<int,int> Edge ;

vector<Edge> e[10005] ;

const int oo = 99999999 ;

int dis[10005] = {} ;

int n , m ;
int ans = 0 ;
void SPFA()
{
for(int i = 0 ; i < n ; i ++)
dis[i] = oo ;
queue<int> q ;
q.push(0) ;
dis[0] = 0 ;
while( !q.empty() )
{
int u = q.front() ; q.pop() ;
for(vector<Edge>::iterator it = e[u].begin() ; it != e[u].end() ; it ++)
{
int v = it->second , w = it->first ;
if( dis[v] > dis[u] + w )
{
dis[v] = dis[u] + w ;
q.push(v) ;
}
}
}

}

void Bfs()
{
bool inq[10005] = {} ;
queue<int> q ;
q.push(n-1);
while(!q.empty() )
{
int v = q.front() ; q.pop() ;
for(vector<Edge>::iterator it = e[v].begin() ; it != e[v].end() ; it ++)
{
int u = it->second , w = it->first ;
if( dis[u] + w == dis[v] )
{
ans += w ;
if(!inq[u])
{
q.push(u);
inq[u] = true ;
}
}
}
}
}

void addedge(int u , int v , int w )
{
e[u].push_back(make_pair(w,v)) ;
e[v].push_back(make_pair(w,u)) ;
}

int main()
{
freopen("in","r",stdin);
freopen("out","w",stdout);

scanf("%d %d",&n,&m);

for(int i = 1 ; i <= m ; i ++)
{
int u , v , w ;
scanf("%d %d %d",&u,&v,&w);
addedge(u,v,w) ;
}

SPFA() ;
Bfs() ;

printf("%d",ans<<1);

return 0;
}
63 changes: 63 additions & 0 deletions 13307130389/assignment13/t2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<sstream>

using namespace std ;

int n , k ;
int fa[50005] = {} , tag[50005] = {} ;

int find( int x )
{
if( fa[x] == x ) return x ;
int t = fa[x] ;
fa[x] = find(fa[x]) ;
tag[x] = (tag[x] + tag[t])%3 ;
return fa[x];
}

void merge(int d , int x ,int y )
{
int fx = find(x) ;
int fy = find(y) ;
fa[fy] = fx ;
tag[fy] = (tag[x]-tag[y]+d-1+3)%3 ;
}

int main()
{
int ans = 0 ;
scanf("%d %d",&n,&k);
for(int i = 1 ; i <= n ; i ++)
{
tag[i] = 0 ;
fa[i] = i ;
}

for(int i = 1 ; i <= k ; i ++)
{
int d , x , y ;
scanf("%d %d %d",&d,&x,&y) ;
if( x > n || y > n || (x==y&&d!=1) )
{
ans ++ ;
continue ;
}
int fx = find(x) , fy = find(y) ;
if( fx == fy )
{
if( (tag[y]-tag[x]+3)%3 != d - 1 ) ans ++ ;
}
else merge(d,x,y);
}
printf("%d",ans);
return 0;
}

66 changes: 66 additions & 0 deletions 13307130389/assignment13/t3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<sstream>
#include<cmath>

using namespace std ;
const double oo = 99999999 ;
double dis[505] = {} ;
bool vis[505] = {} ;
double nd[505][2] = {} ;
double res[505] = {} ;
int rescnt = 0 ;
int s , n ;

double dist( int x , int y )
{
return sqrt( (nd[x][0]-nd[y][0])*(nd[x][0]-nd[y][0])+(nd[x][1]-nd[y][1])*(nd[x][1]-nd[y][1]) ) ;
}

int main()
{
int T ;
scanf("%d",&T);
while(T --)
{
memset(vis,false,sizeof(vis)) ;
rescnt = 0 ;
scanf("%d %d",&s,&n) ;
for(int i = 1 ; i <= n ; i ++)
scanf("%lf %lf",&nd[i][0],&nd[i][1]) ;
for(int i = 1 ; i <= n ; i ++)
dis[i] = oo ;

dis[1] = 0 ; vis[1] = true ;
int next = 1 ;
for(int i = 1 ; i < n ; i ++)
{
double mindis = oo ;
for( int j = 1 ; j <= n ; j ++ ) if( j != i && !vis[j] )
if( dis[j] > dist(next,j) )
{
dis[j] = dist(next,j) ;
}
vis[next] = true ;
for( int j = 1 ; j <= n ; j ++ ) if( !vis[j] )
if( dis[j] < mindis )
{
mindis = dis[j] ;
next = j ;
}
res[++ rescnt] = mindis ;
}
sort( res + 1 , res + n ) ;
printf("%.2f\n",res[n-s]);
}

return 0;
}