杭电ACM-LCY算法进阶培训班-专题训练(04-08-12-16)
1009 Intervals
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define inf 0x7fffffff
//#define ll long long
#define int long long
//#define double long double
#define re int
#define void inline void
#define eps 1e-8
//#define mod 1e9+7
#define ls(p) p<<1
#define rs(p) p<<1|1
#define pi acos(-1.0)
#define pb push_back
#define P pair < int , int >
#define mk make_pair
using namespace std;
const int mod=1e9+7;
const int M=1e8+5;
const int N=2e5+5;//?????????? 4e8
int n,ma,mi,v[N],d[N];
struct ndoe
{
int ver,edge,next;
}e[N];
int tot,head[N];
void add(int x,int y,int z){e[++tot].ver=y;e[tot].edge=z;e[tot].next=head[x];head[x]=tot;}
void init()
{
for(re i=0;i<N;i++) v[i]=head[i]=0,d[i]=-1e18;
tot=1;
ma=0,mi=1e18;
}
int spfa(int s)
{
queue < int > q;q.push(s);d[s]=0;v[s]=1;
while(q.size())
{
int x=q.front();q.pop();v[x]=0;
for(re i=head[x];i;i=e[i].next)
{
int y=e[i].ver;
int z=e[i].edge;
if(d[y]<d[x]+z)
{
d[y]=d[x]+z;
if(!v[y]) v[y]=1,q.push(y);
}
}
}
return d[ma];
}
void solve()
{
while(cin>>n)
{
init();
for(re i=1;i<=n;i++)
{
int x,y,z;
scanf("%lld%lld%lld",&x,&y,&z);
add(x-1,y,z);ma=max(y,ma);mi=min(x-1,mi);
}
for(re i=mi;i<=ma;i++) add(i-1,i,0),add(i,i-1,-1);
cout<<spfa(mi)<<endl;
}
}
signed main()
{
int T=1;
// cin>>T;
for(int index=1;index<=T;index++)
{
// printf("Case #%d: ",index);
solve();
// puts("");
}
return 0;
}
/*
*/
1010 World Exhibition
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define inf 0x7fffffff
//#define ll long long
#define int long long
//#define double long double
#define re int
#define void inline void
#define eps 1e-8
//#define mod 1e9+7
#define ls(p) p<<1
#define rs(p) p<<1|1
#define pi acos(-1.0)
#define pb push_back
#define P pair < int , int >
#define mk make_pair
using namespace std;
const int mod=1e9+7;
const int M=1e8+5;
const int N=2e5+5;//?????????? 4e8
int n,v[N],d[N],cnt[N];
int m1,m2;
struct ndoe
{
int ver,edge,next;
}e[N];
int tot,head[N];
void add(int x,int y,int z){e[++tot].ver=y;e[tot].edge=z;e[tot].next=head[x];head[x]=tot;}
void init()
{
for(re i=0;i<N;i++) v[i]=head[i]=0,d[i]=1e18,cnt[i]=0;
tot=1;
}
int spfa(int s)
{
queue < int > q;q.push(s);d[s]=d[0]=0;v[s]=1;
while(q.size())
{
int x=q.front();q.pop();v[x]=0;
// cout<<x<<endl;
if(cnt[x]>n+2) return -1;
for(re i=head[x];i;i=e[i].next)
{
int y=e[i].ver;
int z=e[i].edge;
if(d[y]>d[x]+z)
{
d[y]=d[x]+z;
if(!v[y]) v[y]=1,q.push(y),cnt[y]=cnt[x]+1;
}
}
}
if(d[n]==1e18) d[n]=-2;
return d[n];
}
void solve()
{
cin>>n>>m1>>m2;
init();
for(re i=1;i<=m1;i++)
{
int x,y,z;
scanf("%lld%lld%lld",&x,&y,&z);
add(x,y,z);
}
for(re i=1;i<=m2;i++)
{
int x,y,z;
scanf("%lld%lld%lld",&x,&y,&z);
add(y,x,-z);
}
for(re i=1;i<=n;i++) add(i,i-1,0);
for(re i=1;i<=n;i++) add(0,i,0);
cout<<spfa(0)<<endl;
}
signed main()
{
int T=1;
cin>>T;
for(int index=1;index<=T;index++)
{
// printf("Case #%d: ",index);
solve();
// puts("");
}
return 0;
}
/*
*/