HDU 6386 Age of Moyu 两种做法 优先队列+拆边

100 篇文章 0 订阅

 

Age of Moyu

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 616    Accepted Submission(s): 142


 

Problem Description

Mr.Quin love fishes so much and Mr.Quin’s city has a nautical system,consisiting of N ports and M shipping lines. The ports are numbered 1 to N . Each line is occupied by a Weitian. Each Weitian has an identification number.

The i -th (1≤i≤M) line connects port Ai and Bi (Ai≠Bi) bidirectionally, and occupied by Ci Weitian (At most one line between two ports).

When Mr.Quin only uses lines that are occupied by the same Weitian, the cost is 1 XiangXiangJi. Whenever Mr.Quin changes to a line that is occupied by a different Weitian from the current line, Mr.Quin is charged an additional cost of 1 XiangXiangJi. In a case where Mr.Quin changed from some Weitian A 's line to another Weitian's line changes to Weitian A 's line again, the additional cost is incurred again.

Mr.Quin is now at port 1 and wants to travel to port N where live many fishes. Find the minimum required XiangXiangJi (If Mr.Quin can’t travel to port N , print −1 instead)

 

 

Input

There might be multiple test cases, no more than 20 . You need to read till the end of input.

For each test case,In the first line, two integers N (2≤N≤100000) and M (0≤M≤200000) , representing the number of ports and shipping lines in the city.

In the following m lines, each contain three integers, the first and second representing two ends Ai and Bi of a shipping line (1≤Ai,Bi≤N) and the third representing the identification number Ci (1≤Ci≤1000000) of Weitian who occupies this shipping line.

 

 

Output

For each test case output the minimum required cost. If Mr.Quin can’t travel to port N , output −1 instead.

 

 

Sample Input

 

3 3 1 2 1 1 3 2 2 3 1 2 0 3 2 1 2 1 2 3 2

 

 

Sample Output

 

1 -1 2

 

 

Source

2018 Multi-University Training Contest 7

 

 

题意:

      给你n个城市,m条边,有个人想从城市1开始走到城市n,但是每条边都有个对应的公司掌管,如果你走的路换了一个不同的公司,那么你的费用要+1,问从1走到n的最小费用(两个城市间最多只有一条路,从公司1到公司2再到公司1也还要加上一次费用)

 

做法1:直接暴力做,用set存下走到这个结点费用最少的有哪几个公司,跑一边最短路就好了。

代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn=100010;
const int inf=0x3f3f3f3f;
typedef long long ll;
typedef pair<ll,ll> pii;
set<ll> app[maxn];
int head[maxn],cnt,n,m;
ll ans[maxn];
priority_queue<pii> q;
struct node{
    int to,next,com;
}e[maxn<<2];
void add(int u,int v,int c){
    e[cnt].to=v,e[cnt].next=head[u];
    e[cnt].com=c,head[u]=cnt++;
}
void init(){
    cnt=0;
    for(int i=0;i<=n;i++){
        app[i].clear();
        head[i]=-1;
        ans[i]=inf;
    }
}
void bfs(){
    while(!q.empty()){
        int now=q.top().second,dis=-q.top().first;
        q.pop();
        if(ans[now]>dis) continue;
        for(int i=head[now];~i;i=e[i].next){
            int to=e[i].to,nowcom=e[i].com;
            int newdis=dis+(!app[now].count(nowcom));
            if(ans[to]>newdis){
                ans[to]=newdis;
                q.push(pii(-newdis,to));
                app[to].clear();
                app[to].insert(nowcom);
            }
            else if(ans[to]==newdis){
                app[to].insert(nowcom);
            }
        }
    }
}
int main(){
    int x,y,z;
	while(~scanf("%d%d",&n,&m)){
        init();
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);  add(y,x,z);
        }
        ans[1]=0; q.push(pii(0,1));
        bfs();
        printf("%d\n",ans[n]>=inf?-1:ans[n]);
	}
	return 0;
}

 

 

做法2:拆边!!

      将边(pi,qi,ci)拆为(pi,ci)和(qi,ci)两个点,在pi、(pi,ci),qi、(qi,ci),(pi,ci)、(qi,ci)之间连边,并将权值分别赋为1,1,0,意思就是在pi上车需要1元,在pi,qi之间坐车消耗0元,在qi下车又需要1元。

但是这个题的数据比较大,又是多组样例,所以好像怎么做都是T...心累累,所以接下来的代码是过不了的,但是这个做法我觉得很好,想保存下来。请看到的宝宝不要介意。。。

 

代码如下:注意!!过不了

 

#include<bits/stdc++.h>
using namespace std;
const int maxn=300005;
const int inf=0x3f3f3f3f;
typedef pair<int,int> pii;
int dist[maxn],vis[maxn],n,m,cnt;
vector<pii> edge[maxn];
map<pii, int > mp;
int gainp(int city,int com){
    if(!mp[pii(city,com)]) mp[pii(city,com)]=++cnt;
    return mp[pii(city,com)];
}
void add(int u,int v,int c){
    edge[u].push_back(pii(v,c));
    edge[v].push_back(pii(u,c));
}
void spfa(int st){
    for(int i=1;i<=cnt;i++)
        dist[i]=inf,vis[i]=0;
    queue<int> q;
    q.push(st);
    vis[st]=1; dist[st]=0;
    while(!q.empty()){
        int u=q.front(); q.pop();
        vis[u]=0;
        for(int i=0;i<edge[u].size();i++){
            int v=edge[u][i].first,w=edge[u][i].second;
            if(dist[v]>dist[u]+w){
                dist[v]=dist[u]+w;
                if(!vis[v]) {
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
}
int main(){
    int u,v,c;
    while(~scanf("%d%d",&n,&m)){
        mp.clear();
        for(int i=0;i<=maxn;i++)
            edge[i].clear();
        cnt=n;
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&u,&v,&c);
            int uu=gainp(u,c),vv=gainp(v,c);
            add(u,uu,1);  add(uu,vv,0);  add(vv,v,1);
        }
        spfa(1);
        printf("%d\n",dist[n]>=inf?-1:dist[n]/2);

    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值