|
楼主 |
发表于 2021-10-22 16:28:03
|
显示全部楼层
T3
- #include<bits/stdc++.h>
- #define int long long
- #define NUM (int)2e6+1
- #define EDGE (int)1e6+1
- #define POINT (int)1e5+1
- #define hash hashh
- #define map mapp
- using namespace std;
- int N,T,E;
- int x,f,u,v,l;
- int cnt,root,head[POINT],father[POINT];
- int mapcnt,map[NUM];
- int dp[POINT],res=1e18,resx;
- struct edge{
- int to,len,next;
- }e[EDGE];
- inline int hash(int p){
- return map[p]?map[p]:(map[p]=++mapcnt);
- }
- inline void link(int u,int v,int w){
- cnt++;
- e[cnt].next=head[u],e[cnt].len=w,e[cnt].to=v;
- head[u]=cnt;
- }
- inline bool relax(int u,int v,int w){
- if (dp[v]>dp[u]+w){
- dp[v]=dp[u]+w;
- return true;
- }
- return false;
- }
- void dfs(int p){
- for (int i=head[p];i;i=e[i].next)
- if (relax(p,e[i].to,e[i].len))
- dfs(e[i].to);
- }
- signed main(){
- //freopen("river.in","r",stdin);
- //freopen("river.out","w",stdout);
- scanf("%lld%lld%lld",&N,&T,&E);
- for (int i=1;i<N;i++){
- scanf("%lld%lld%lld",&x,&f,&l);
- link(hash(f),hash(x),l);
- father[hash(x)]=hash(f);
- }
-
- int pos=hash(f);
- while (father[pos])
- pos=father[pos];
- root=pos;
-
- memset(dp,0x3f,sizeof(dp));
- dp[root]=0;
- dfs(root);
-
- E=hash(E);
- for (int i=1;i<=T;i++){
- scanf("%lld%lld%lld",&u,&v,&l);
- u=hash(u),v=hash(v);
- link(u,v,l);
- if (relax(u,v,l))
- dfs(v);
- if (res>i*i+dp[E])
- res=i*i+dp[E],resx=i;
- }
-
- if (res==1e18) res=-1;
- printf("%lld\n%lld",resx,res);
- //fclose(stdin);
- //fclose(stdout);
- return 0;
- }
复制代码 |
|