diff --git a/13307130389/assignment13/t1.cpp b/13307130389/assignment13/t1.cpp new file mode 100644 index 0000000..a0d4c67 --- /dev/null +++ b/13307130389/assignment13/t1.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std ; + +typedef pair Edge ; + +vector 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 q ; + q.push(0) ; + dis[0] = 0 ; + while( !q.empty() ) + { + int u = q.front() ; q.pop() ; + for(vector::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 q ; + q.push(n-1); + while(!q.empty() ) + { + int v = q.front() ; q.pop() ; + for(vector::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; +} diff --git a/13307130389/assignment13/t2.cpp b/13307130389/assignment13/t2.cpp new file mode 100644 index 0000000..06ddefc --- /dev/null +++ b/13307130389/assignment13/t2.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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; +} + diff --git a/13307130389/assignment13/t3.cpp b/13307130389/assignment13/t3.cpp new file mode 100644 index 0000000..98e0e24 --- /dev/null +++ b/13307130389/assignment13/t3.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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; +} +