题意:求缩点后的DAG图中到所有点的最小距离
解法:直接套模版 然后枚举每一条边 获得到达该联通分量的最小值
要注意 如果没有边到达该点的话 那么这个点是可以直接到达的 所以它的距离应该算作0
奇怪的是 如果我根据点来枚举每一条边,那么是可以Ac的
但是我直接枚举每一条边的时候却哇了
希望路过的神牛不吝赐教
根据点枚举边:
#pragma comment(linker,"/STACK:102400000,102400000")
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long ll; //记得必要的时候改成无符号
const int maxn=100005; //点数
const int maxm=500005; //边数
const int INF=100005555;
int scan()
{
int res=0,ch;
while(!((ch= getchar())>='0'&&ch<='9')){
if(ch==EOF)return 1<<30;
}
res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')
res=res*10+(ch-'0');
return res;
}
struct EdgeNode
{
int from;
int to;
int next,value;
}edge[maxm];
int head[maxn],cnt;
void add(int x,int y,int z)
{
edge[cnt].from=x;edge[cnt].to=y;
edge[cnt].value=z;
edge[cnt].next=head[x];head[x]=cnt++;
}
void init()
{
cnt=1;
memset(head,-1,sizeof(head));
}
int dfn[maxn],low[maxn],ins[maxn];
int cixu,scc_count,n,ru[maxn],chu[maxn],sd[maxn];
stack<int>S;
void dfs(int u)
{
int v,x;
dfn[u]=low[u]=++cixu;
S.push(u);
ins[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].to;
if(!dfn[v]){
dfs(v);
low[u]=min(low[u],low[v]);
}
else if(ins[v])
//如果已经搜过,并且在栈里面,如果不在栈里面,说明该点属于其他的强连通分量里面
low[u]=min(low[u],dfn[v]);
//此处low代替dfn也是可以的,但是在tarjan求无向图的时候不能
}
if(dfn[u]==low[u])
//表示u为此强连通分量第一个被发现的点,属于该强连通分量的所有的其他点都在栈中,他的上面
{
scc_count++;
do
{
x=S.top();
S.pop();
sd[x]=scc_count;
ins[x]=0;
}while(x!=u);
}
}
void tarjan()
{
for(int i=1;i<=n;i++)sd[i]=i; //缩点标记数组
memset(ins,0,sizeof(ins));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
cixu=scc_count=0;
for(int i=1;i<=n;i++) //可能所给的图为不连通的图,分块,因此需要遍历所有的点
{
if(!dfn[i])
dfs(i);
}
}
int value[maxn],va[maxn];
// scc_count 1...
int main()
{
int m,i,p,sum,x,y,z;
while(~scanf("%d%d",&n,&m)){
init(); sum=0; p=0;
for(i=1;i<=m;i++){
x=scan();y=scan();z=scan();
++x;++y;
add(x,y,z);
}
tarjan();
for(int i=1;i<=n;++i)value[i]=INF;
// value[1]=0;
for(int i=1;i<=n;++i)
for(int j=head[i];j!=-1;j=edge[j].next){
int v=sd[edge[j].to],u=sd[i];
if(u!=v){
value[v]=min(value[v],edge[j].value);
}
}
int ans=0;
for(int i=1;i<=scc_count;++i)if(value[i]!=INF)ans+=value[i];
printf("%d\n",ans);
}
return 0;
}
直接枚举边 (WA)
#pragma comment(linker,"/STACK:102400000,102400000")
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long ll; //记得必要的时候改成无符号
const int maxn=100005; //点数
const int maxm=500005; //边数
const int INF=100005555;
int scan()
{
int res=0,ch;
while(!((ch= getchar())>='0'&&ch<='9')){
if(ch==EOF)return 1<<30;
}
res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')
res=res*10+(ch-'0');
return res;
}
struct EdgeNode
{
int from;
int to;
int next,value;
}edge[maxm];
int head[maxn],cnt;
void add(int x,int y,int z)
{
edge[cnt].from=x;edge[cnt].to=y;
edge[cnt].value=z;
edge[cnt].next=head[x];head[x]=cnt++;
}
void init()
{
cnt=1;
memset(head,-1,sizeof(head));
}
int dfn[maxn],low[maxn],ins[maxn];
int cixu,scc_count,n,ru[maxn],chu[maxn],sd[maxn];
stack<int>S;
void dfs(int u)
{
int v,x;
dfn[u]=low[u]=++cixu;
S.push(u);
ins[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].to;
if(!dfn[v]){
dfs(v);
low[u]=min(low[u],low[v]);
}
else if(ins[v])
//如果已经搜过,并且在栈里面,如果不在栈里面,说明该点属于其他的强连通分量里面
low[u]=min(low[u],dfn[v]);
//此处low代替dfn也是可以的,但是在tarjan求无向图的时候不能
}
if(dfn[u]==low[u])
//表示u为此强连通分量第一个被发现的点,属于该强连通分量的所有的其他点都在栈中,他的上面
{
scc_count++;
do
{
x=S.top();
S.pop();
sd[x]=scc_count;
ins[x]=0;
}while(x!=u);
}
}
void tarjan()
{
for(int i=1;i<=n;i++)sd[i]=i; //缩点标记数组
memset(ins,0,sizeof(ins));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
cixu=scc_count=0;
for(int i=1;i<=n;i++) //可能所给的图为不连通的图,分块,因此需要遍历所有的点
{
if(!dfn[i])
dfs(i);
}
}
int value[maxn],va[maxn];
// scc_count 1...
int main()
{
int m,i,p,sum,x,y,z;
while(~scanf("%d%d",&n,&m)){
init(); sum=0; p=0;
for(i=1;i<=m;i++){
x=scan();y=scan();z=scan();
++x;++y;
add(x,y,z);
}
tarjan();
for(int i=1;i<=n;++i)value[i]=INF;
for(int i=1;i<=cnt;++i){
int u=sd[edge[i].from],v=sd[edge[i].to];
if(u!=v){
value[v]=min(value[v],edge[i].value);
}
}
int ans=0;
for(int i=1;i<=scc_count;++i)if(value[i]!=INF)ans+=value[i];
printf("%d\n",ans);
}
return 0;
}