Codeforces Round #374 (Div. 2)

记录一个菜逼的成长。。

A,B水题。
C. Journey
题目大意:
给你N个点M条边的DAG,有T的时间限制.接下来M行(u,v,t)代表从u到v的时间为t。你站在1点 ,问到n点,不超过T时间最多走过几个点,并输出路径。

跟拓扑排序很像,加上dp。
dp[i][j] := 站在i点,走过j个点的最短时间。
显然dp[i][j] = min(dp[i][j] + t[i],dp[to[i]][j+1]);
path[i][j] := 站在i点,走过j个点的前面那个点。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define ALL(v) (v).begin(),(v).end()
#define cl(a,b) memset(a,b,sizeof(a))
#define bp __builtin_popcount
#define pb push_back
#define fin freopen("D://in.txt","r",stdin)
#define fout freopen("D://out.txt","w",stdout)
#define lson t<<1,l,mid
#define rson t<<1|1,mid+1,r
#define seglen (node[t].r-node[t].l+1)
#define pi 3.1415926
#define e  2.718281828459
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair PLL;
typedef vector VPII;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
template <typename T>
inline void read(T &x){
    T ans=0;
    char last=' ',ch=getchar();
    while(ch<'0' || ch>'9')last=ch,ch=getchar();
    while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
    if(last=='-')ans=-ans;
    x = ans;
}
inline bool DBread(double &num)
{
    char in;double Dec=0.1;
    bool IsN=false,IsD=false;
    in=getchar();
    if(in==EOF) return false;
    while(in!='-'&&in!='.'&&(in<'0'||in>'9'))
        in=getchar();
    if(in=='-'){IsN=true;num=0;}
    else if(in=='.'){IsD=true;num=0;}
    else num=in-'0';
    if(!IsD){
        while(in=getchar(),in>='0'&&in<='9'){
            num*=10;num+=in-'0';}
    }
    if(in!='.'){
        if(IsN) num=-num;
            return true;
    }else{
        while(in=getchar(),in>='0'&&in<='9'){
                num+=Dec*(in-'0');Dec*=0.1;
        }
    }
    if(IsN) num=-num;
    return true;
}
template <typename T>
inline void write(T a) {
    if(a < 0) { putchar('-'); a = -a; }
    if(a >= 10) write(a / 10);
    putchar(a % 10 + '0');
}
/******************head***********************/
const int maxn = 5000 + 10;
int head[maxn],cnt;
int path[maxn][maxn],dp[maxn][maxn],indegree[maxn];
int n,m,T;
struct Edge{
    int to,next,w;
}edge[maxn];
void add(int u,int v,int w)
{
    edge[cnt].to = v;
    edge[cnt].w = w;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}
void init()
{
    cl(head,-1);
    cl(path,0);
    cl(indegree,0);
    for( int i = 1; i <= n; i++ ){
        for( int j = 1; j <= n; j++ ){
            dp[i][j] = INF;
        }
    }
}
void print_path(int num)
{
    stack<int>s;
    int i = n,j = num;
    while(path[i][j]){
        s.push(i);
        i = path[i][j];
        j--;
    }
    int flag = 1;
    s.push(1);
    while(!s.empty()){
        if(flag){
            printf("%d",s.top());
            s.pop();
            flag = 0;
        }
        else {
            printf(" %d",s.top());
            s.pop();
        }
    }
    puts("");
}
void solve()
{
    queue<int>q;
    dp[1][1] = 0;
    for( int i = 1; i <= n; i++ )
        if(!indegree[i])q.push(i);
    while(!q.empty()){
        int f = q.front();q.pop();
        for( int i = head[f]; ~i; i = edge[i].next){
            int v = edge[i].to,w = edge[i].w;
            for( int j = 1; j <= n; j++ ){
                if(dp[f][j] + w < dp[v][j+1]){
                    dp[v][j+1] = dp[f][j] + w;
                    path[v][j+1] = f;
                }
            }
            if(!--indegree[v])
                q.push(v);
        }
    }
    for( int i = n; i > 0; i-- ){
        if(dp[n][i] <= T){
            printf("%d\n",i);
            print_path(i);
            break;
        }
    }
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&T)){
        init();
        for( int i = 0; i < m; i++ ){
            int u,v,t;
            scanf("%d%d%d",&u,&v,&t);
            add(u,v,t);
            indegree[v]++;
        }
        solve();
    }
    return 0;
}

你可能感兴趣的:(dp,codeforces,gragh,图论,topsort,codeforces,dp,topsort,gragh)