COGS 133. [USACO Mar08] 牛跑步(洛谷 P2901)

133. [USACO Mar08] 牛跑步

★★★ 输入文件:cowjog.in 输出文件:cowjog.out 简单对比
时间限制:1 s 内存限制:128 MB
Bessie准备用从牛棚跑到池塘的方法来锻炼. 但是因为她懒,她只准备沿着下坡的路跑到池塘,然后走回牛棚.
Bessie也不想跑得太远,所以她想走最短的路经. 农场上一共有M(1<=M<=10,000)条路,每条路连接两个用1..N(1<=N<=1000)标号的地点. 更方便的是,如果X>Y,则地点X的高度大于地点Y的高度. 地点N是Bessie的牛棚;地点1是池塘.
很快, Bessie厌倦了一直走同一条路.所以她想走不同的路,更明确地讲,她想找出K(1<=K<=100)条不同的路经.为了避免过度劳累,她想使这K条路径为最短的K条路径.
请帮助Bessie找出这K条最短路经的长度.你的程序需要读入农场的地图, 一些从Xi到Yi的路径和它们的长度(Xi,Yi,Di). 所有(Xi,Yi,Di)满足(1<=Yi

//啥!这题还有拓扑排序版!
#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
using namespace std;
const int N = 1005;
priority_queue<int> que[N];
int n,m,k,in[N];
int head[N],tot=1;
struct Edge{ int to,w,next; }e[N*10];
void Add_Egde(int u,int v,int w){
    e[tot].to=v;e[tot].w=w;
    e[tot].next=head[u];head[u]=tot++;
}
stack<int> st;
int tmp[105],t=0;
inline int read(){
    char c=getchar();
    int x=0,f=1;
    while(c<'0'||c>'9'){
        if(c=='-') f=-1,c=getchar();
    }
    while(c>='0'&&c<='9'){
        x=(x<<3)+(x<<1)+(c-'0');
        c=getchar();
    }
    return x*f;
}
void ToPo(){
    for(int i=1;i<=n;i++) if(!in[i]) st.push(i);
    que[n].push(0);
    while(!st.empty()){
        int v=st.top();st.pop();
        if(v==1){ break; }
        t=0;
        while(!que[v].empty()){
            tmp[++t]=que[v].top();
            que[v].pop();
        }
        for(int i=head[v];i;i=e[i].next){
            int p=e[i].to;
            for(int j=1;j<=t;j++)
                que[p].push(tmp[j]+e[i].w);
            while(que[p].size()>k) que[p].pop();
            if(!--in[p]) st.push(p);
        }
    }
    int r=k-que[1].size();
    t=0;
    while(!que[1].empty()){
        tmp[++t]=que[1].top();que[1].pop();
    }
    while(t){ printf("%d",tmp[t]); t--; }
    for(int i=0;i<r;i++) printf("%d\n",-1);
}
int main(){
    freopen("cowjog.in","r",stdin);
    freopen("cowjog.out","w",stdout);
    n=read();m=read();k=read();
    for(int u,v,w,i=1;i<=m;i++){
        u=read();v=read();w=read();
        Add_Egde(u,v,w);
        in[v]++;
    }
    ToPo();
    return 0;
}
//40分 暴力 
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxm 10010
#define maxn 1010
struct Edge{ int u,v,w,next; }e[maxm*2];
int head[maxn],n,m,k,tot,anscnt;
int ans[maxn*20000];
inline int read(){
    char c=getchar();
    int x=0,f=1;
    while(c<'0'||c>'9'){
        if(c=='-') f=-1,c=getchar();
    }
    while(c>='0'&&c<='9'){
        x=(x<<3)+(x<<1)+(c-'0');
        c=getchar();
    }
    return x*f;
}
inline void Add_Edge(int u,int v,int w){
    e[++tot].u=u;e[tot].v=v;e[tot].w=w;
    e[tot].next=head[u];head[u]=tot;
}
inline void DFS(int u,int fa,int w){
    if(u==1){
        ans[++anscnt]=w;
        return ;
    }
    for(int i=head[u];i;i=e[i].next){
        int v=e[i].v;
        if(v!=fa&&u>v) DFS(v,u,w+e[i].w);
    }
    return ;
}
int main(){
    freopen("cowjog.in","r",stdin);
    freopen("cowjog.out","w",stdout);
    n=read();m=read();k=read();
    for(int u,v,w,i=1;i<=m;i++){
        u=read();v=read();w=read();
        Add_Edge(u,v,w);
    }
    DFS(n,0,0);
    sort(ans+1,ans+1+anscnt);
    for(int i=1;i<=k;i++){
        if(ans[i]) printf("%d\n",ans[i]);
        else cout<<"-1"<<endl;
    }
    return 0;
}
//A*搜索 
#include<iostream>
#include<cmath> 
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
#define maxn 20010
const int INF = 0x3f3f3f3f;
struct Edge{ int v,w,next; }e[maxn<<1],ee[maxn<<1];
int head[maxn],n,m,k,tot;
int linkk[maxn],link_tot;
int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void Add_Edge(int u,int v,int w){
    e[++tot].v=v;e[tot].w=w;
    e[tot].next=head[u];head[u]=tot;
    ee[++link_tot].v=u;ee[link_tot].w=w;
    ee[link_tot].next=linkk[v];linkk[v]=link_tot;
}
struct Node{
    int u,dis;
    Node(int a,int b){ u=a; dis=b; }
    friend bool operator < (const Node &a,const Node &b){
        return a.dis>b.dis;
    }
};
priority_queue<Node>q;
int dis[maxn];
void Dijkstra(){
    memset(dis,0x3f,sizeof dis );
    dis[n]=0;q.push(Node(n,0));
    while(!q.empty()){
        Node P=q.top();q.pop();
        while(P.dis>dis[P.u]){
            if(q.empty()) break;
            P=q.top();q.pop();
        }
        int u=P.u;
        for(int i=head[u];i;i=e[i].next){
            int v=e[i].v;
            if(dis[v]>dis[u]+e[i].w){
                dis[v]=dis[u]+e[i].w;
                q.push((Node){v,dis[v]});
            }
        }
    }
    return ;
}
int sum=0;
struct cmp{
    bool operator () (const Node a,const Node b){
        return a.dis+dis[a.u] > b.dis+dis[b.u];
    }
};
priority_queue<Node,vector<Node>,cmp> que;
void Solve(){
    que.push(Node(1,0));
    while(!que.empty()){
        Node P=que.top();que.pop();
        while(P.u == n){
            sum++;
            printf("%d\n",P.dis);
            if(sum==k||que.empty()) return ;
            P=que.top();que.pop();
        }
        int u=P.u;
        for(int i=linkk[u];i;i=ee[i].next){
            int v=ee[i].v;
            que.push(Node(v,P.dis+ee[i].w));
        }
    }
    return ;
}
int main(){
    scanf("%d%d%d",&n,&m,&k);
    for(int u,v,w,i=1;i<=m;i++){
        scanf("%d%d%d",&u,&v,&w);
        (u>v) ? Add_Edge(u,v,w) : Add_Edge(v,u,w);
    }
    Dijkstra();
    Solve();
    for(int i=sum+1;i<=k;i++) printf("-1\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是将代码修改为cot平滑的方法: 1. 首先,需要使用边界角的cot权重计算每个顶点的权重。 2. 然后,使用cot权重对每个顶点的邻域点进行加权计算,得到平滑后的坐标。 3. 最后,根据平滑后的坐标更新每个顶点的位置。 修改后的代码如下: float smooth() { float err = -1; cogs.clear(); v_end = mesh.vertices_end(); //cot平滑 for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it) { cog[0] = cog[1] = cog[2] = weight_sum = 0.0; for (vv_it = mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it) { double cot_weight = 0.0; MyMesh::HalfedgeHandle heh = mesh.find_halfedge(*v_it, *vv_it); if (!mesh.is_boundary(heh)) { MyMesh::HalfedgeHandle prev_heh = mesh.prev_halfedge_handle(heh); MyMesh::HalfedgeHandle next_heh = mesh.next_halfedge_handle(heh); MyMesh::VertexHandle prev_vh = mesh.to_vertex_handle(prev_heh); MyMesh::VertexHandle next_vh = mesh.to_vertex_handle(next_heh); MyMesh::Point prev_p = mesh.point(prev_vh); MyMesh::Point curr_p = mesh.point(*v_it); MyMesh::Point next_p = mesh.point(next_vh); double cot_alpha = cot(prev_p - curr_p, next_p - curr_p); double cot_beta = cot(curr_p - prev_p, next_p - prev_p); cot_weight = cot_alpha + cot_beta; } cog += cot_weight * mesh.point(*vv_it); weight_sum += cot_weight; } cogs.push_back(cog / weight_sum); } for (v_it = mesh.vertices_begin(), cog_it = cogs.begin(); v_it != v_end; ++v_it, ++cog_it) { if (!mesh.is_boundary(*v_it)) { MyMesh::Point p = mesh.point(*v_it); err = max(err, (p - *cog_it).norm()); mesh.set_point(*v_it, *cog_it); } } return err; } 其中cot函数的定义如下: double cot(MyMesh::Point a, MyMesh::Point b) { return dot(a, b) / cross(a, b).norm(); } 注意,这里使用的是边界角的cot权重,因此在计算cot权重时需要判断当前边是否为边界。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值